Example #1
0
        //Begin the game
        private static void beginGame(identity firstID, ref Player xPlayer, ref Player oPlayer, ref GameBoard Game,
                                      ref Move move)
        {
            Console.Write("Enter Game Name: ");
            string gameName = Console.ReadLine();

            Player currentPlayer = xPlayer;

            if (firstID == identity.X)
            {
                currentPlayer = xPlayer;
            }
            else
            {
                currentPlayer = oPlayer;
            }

            bool     AIAutoMove       = false;
            bool     playerAlwaysMove = false;
            identity AIIdentity       = identity.O;

            Stack <Board> previousMoves = new Stack <Board>();

            while (!Game.gameOver())
            {
                // Console.Write("AI Favors at " + global::AI.AICore.evaluate1(true, game.getBoard()) + "\n");
                //Console.Write(currentPlayer.getIdentity() + " to move...\n");
                char keyCode = ' ';
                if (AIAutoMove && currentPlayer.getIdentity() == AIIdentity)
                {
                    Console.Write("\nRequesting Move From AI");
                    move = AIPlayer.getAMove((currentPlayer.getIdentity() == firstID));
                }
                else
                {
                    if (playerAlwaysMove && currentPlayer.getIdentity() != AIIdentity)
                    {
                        keyCode = 'm';
                    }
                    Console.Write("m = make move\t a = call AI\t n = always make move for human\t q = toggle auto call AI\nt = change AI (from " + AIIdentity +
                                  ")\ti = swap turns\t l = load board\t s = save board\t u = undo\t f = dump memory\t \n");
                    while (keyCode != 'm' && keyCode != 'a' && keyCode != 's' && keyCode != 'l' && keyCode != 'q' && keyCode != 'f' && keyCode != 'i' && keyCode != 'n' && keyCode != 't' && keyCode != 'u')
                    {
                        Console.Write((char)currentPlayer.getIdentity() + ":");
                        keyCode = Console.ReadKey().KeyChar;
                        Console.Write("\n");
                    }

                    if (keyCode == 'm')
                    {
                        Console.Write("Move: ");
                        String line = "        ";
                        while (line.Length != 0 && (line.Length != 4 || !(Char.IsDigit(line[1]) && Char.IsDigit(line[3]) && (line[0] - 97) >= 0 && (line[0] - 97) <= 7 && (line[2] - 97) >= 0 && (line[2] - 97) <= 7)))
                        {
                            line = Console.ReadLine();
                        }
                        if (line.Length != 0)
                        {
                            move         = new Move();
                            move.Begin.Y = line[0] - 97;
                            move.Begin.X = line[1] - 48 - 1;
                            move.End.Y   = line[2] - 97;
                            move.End.X   = line[3] - 48 - 1;
                        }
                        else
                        {
                            move             = null;
                            playerAlwaysMove = false;
                        }
                    }
                    else if (keyCode == 'f')
                    {
                        AIPlayer.EmptyMemory();
                    }
                    else if (keyCode == 't')
                    {
                        if (AIIdentity == identity.X)
                        {
                            AIIdentity = identity.O;
                        }
                        else
                        {
                            AIIdentity = identity.X;
                        }
                    }
                    else if (keyCode == 'n')
                    {
                        if (playerAlwaysMove)
                        {
                            playerAlwaysMove = false;
                        }
                        else
                        {
                            playerAlwaysMove = true;
                            Console.Write("Player always move enabled. Enter an empty move to disable.\nPress enter to aknowledge and continue...");
                            Console.ReadLine();
                        }
                        move = null;
                    }
                    else if (keyCode == 'q')
                    {
                        if (AIAutoMove)
                        {
                            AIAutoMove = false;
                        }
                        else
                        {
                            AIAutoMove = true;
                        }
                        move = null;
                    }
                    else if (keyCode == 'a')
                    {
                        Console.Write("\nRequesting Move From AI");
                        move = AIPlayer.getAMove((currentPlayer.getIdentity() == firstID));
                    }
                    else if (keyCode == 's')
                    {
                        savedBoard  = new Board(game.board);
                        savedPlayer = currentPlayer;
                        move        = null;
                    }
                    else if (keyCode == 'l')
                    {
                        Game.board    = new Board(savedBoard);
                        currentPlayer = savedPlayer;
                        move          = null;
                    }
                    else if (keyCode == 'u')
                    {
                        if (previousMoves.Count != 0)
                        {
                            Game.board = new Board(previousMoves.Pop());
                            if (currentPlayer.getIdentity() == identity.X)
                            {
                                currentPlayer = oPlayer;
                            }
                            else
                            {
                                currentPlayer = xPlayer;
                            }
                            move = null;
                        }
                    }
                    else if (keyCode == 'i')
                    {
                        move = null;
                        if (currentPlayer.getIdentity() == identity.X)
                        {
                            currentPlayer = oPlayer;
                        }
                        else
                        {
                            currentPlayer = xPlayer;
                        }
                    }
                }

                bool moved = false;
                if (move != null)
                {
                    moved = Game.movePiece(currentPlayer.getIdentity(), move);
                }

                //Print the new game state to the screen
                Console.Clear();
                string moveString = "";
                if (move != null)
                {
                    char moveBegin = (char)(move.Begin.Y + 97);
                    char moveEnd   = (char)(move.End.Y + 97);
                    moveString = moveBegin + "" + (move.Begin.X + 1) + "" + moveEnd + "" + (move.End.X + 1);
                    Console.Out.Write(moveString + "\n");
                }
                Game.printGameBoard();

                //Log the ouput
                if (currentPlayer.getIdentity() == identity.O)
                {
                    File.AppendAllText(gameName + ".txt", moveString + Environment.NewLine);
                }
                else
                {
                    File.AppendAllText(gameName + ".txt", moveString + "\t");
                }

                //Log the ouput readable
                if (currentPlayer.getIdentity() == identity.O)
                {
                    File.AppendAllText(gameName + ".read.txt", moveString + Environment.NewLine);
                    File.AppendAllText(gameName + ".read.txt", game.gameBoardString() + Environment.NewLine);
                    File.AppendAllText(gameName + ".read.txt", Environment.NewLine);
                }
                else
                {
                    File.AppendAllText(gameName + ".read.txt", moveString + Environment.NewLine);
                    File.AppendAllText(gameName + ".read.txt", game.gameBoardString() + Environment.NewLine);
                    File.AppendAllText(gameName + ".read.txt", Environment.NewLine);
                }


                if (moved)
                {
                    previousMoves.Push(new Board(game.board));

                    if (currentPlayer.getIdentity() == identity.X)
                    {
                        currentPlayer = oPlayer;
                    }
                    else
                    {
                        currentPlayer = xPlayer;
                    }
                }
            }
        }