Esempio n. 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("********Welcome to the game Tic-Tac-Toe!**********");
            TicTacToeBoard tttBoard = new TicTacToeBoard();

            int mode = tttBoard.ModeOfGame();

            switch (mode)
            {
            case 1:
                ModePlayer1VsPlayer2(tttBoard);
                break;

            case 2:
                Console.WriteLine("\nDo you want to write the results of the game to a file (y - Yes): ");
                string yesOrNo = Console.ReadLine();

                // регистрируем событие
                if (yesOrNo.ToLower().Equals("y"))
                {
                    tttBoard.WriteToFile += ResultToFile;
                }
                ModePlayer1VsComp(tttBoard);
                break;

            default:
                Console.WriteLine("Error of choice!");
                break;
            }
        }
        //Method that creates a message box with buttons
        //Used to ask the user if they want to play again or not
        private void playAgainMessage()
        {
            //Creates a message box with buttons
            MessageBoxResult playAgainBoxResult =
                MessageBox.Show("Do you want to play again?", "Play Again", MessageBoxButton.YesNo);

            //If user says yes then it clears the board
            if (playAgainBoxResult == MessageBoxResult.Yes)
            {
                foreach (Button btn in board)
                {
                    btn.Content = "";
                }

                gameOver    = false;
                gameBoard   = null;
                gameBoard   = new TicTacToeBoard((int)Application.Current.Properties["BoardSize"], mode);
                playerTurn  = true;
                moveCounter = 0;
                oneMoreTurn = false;
            }
            else if (playAgainBoxResult == MessageBoxResult.No) //If user says no then it closes window and opens main window
            {
                MainWindow mainWindow = new MainWindow();

                mainWindow.Show();

                //Sets background and foreground color
                mainWindow.mainGrid.Background   = (Brush)Application.Current.Properties["Background"];
                mainWindow.title.Foreground      = (Brush)Application.Current.Properties["FontColor"];
                mainWindow.boardLabel.Foreground = (Brush)Application.Current.Properties["FontColor"];

                this.Close();
            }
        }
Esempio n. 3
0
 private void button1_Click(object sender, EventArgs e)
 {
     mainBoard           = new TicTacToeBoard();
     mainBoard.Mode      = CheckMode();
     mainBoard.FirstMove = CheckWhoStarts();
     DrawBoard();
     pictureBox1.Visible = true;
     pictureBox2.Visible = true;
     pictureBox3.Visible = true;
     pictureBox4.Visible = true;
     pictureBox5.Visible = true;
     pictureBox6.Visible = true;
     pictureBox7.Visible = true;
     pictureBox8.Visible = true;
     pictureBox9.Visible = true;
     if (mainBoard.FirstMove == 2)
     {
         string[,] board = mainBoard.FirstComputerMove();
         DrawBoard();
     }
     if (mainBoard.Mode == 3)
     {
         button2.Enabled = true;
         mainBoard.ComputerVsComputer();
         DrawBoard();
     }
 }
 public RegularWindow(string gameMode)
 {
     mode = gameMode;
     InitializeComponent();
     gameBoard = new TicTacToeBoard(3, gameMode);
     // MessageBox.Show("Player X is going first.");
 }
 public CustomWindow(string gameMode, TicTacToeBoard boardFromFile) : this(gameMode)
 {
     mode         = gameMode;
     gameBoard    = boardFromFile;
     loadFromSave = true;
     InitializeComponent();
 }
Esempio n. 6
0
 public ThreeDWindow(string gameMode)
 {
     mode = gameMode;
     InitializeComponent();
     gameBoard          = new TicTacToeBoard(true);
     gameBoard.gameMode = "3D";
     // MessageBox.Show("Player X is going first.");
 }
        public RegularWindow(string gameMode, TicTacToeBoard board)
        {
            InitializeComponent();

            mode      = gameMode;
            gameBoard = board;
            Button btn;

            if (gameBoard.Grid[0, 0].symbol != '\0')
            {
                // Button btn = (Button)Application.
                btn         = btn1;
                btn.Content = gameBoard.Grid[0, 0].symbol;
            }
            if (gameBoard.Grid[1, 0].symbol != '\0')
            {
                btn         = btn2;
                btn.Content = gameBoard.Grid[1, 0].symbol;
            }
            if (gameBoard.Grid[2, 0].symbol != '\0')
            {
                btn         = btn3;
                btn.Content = gameBoard.Grid[2, 0].symbol;
            }
            if (gameBoard.Grid[0, 1].symbol != '\0')
            {
                btn         = btn4;
                btn.Content = gameBoard.Grid[0, 1].symbol;
            }
            if (gameBoard.Grid[1, 1].symbol != '\0')
            {
                btn         = btn5;
                btn.Content = gameBoard.Grid[1, 1].symbol;
            }
            if (gameBoard.Grid[2, 1].symbol != '\0')
            {
                btn         = btn6;
                btn.Content = gameBoard.Grid[2, 1].symbol;
            }
            if (gameBoard.Grid[0, 2].symbol != '\0')
            {
                btn         = btn7;
                btn.Content = gameBoard.Grid[0, 2].symbol;
            }
            if (gameBoard.Grid[1, 2].symbol != '\0')
            {
                btn         = btn8;
                btn.Content = gameBoard.Grid[1, 2].symbol;
            }
            if (gameBoard.Grid[2, 2].symbol != '\0')
            {
                btn         = btn9;
                btn.Content = gameBoard.Grid[2, 2].symbol;
            }

            // MessageBox.Show("Player X is going first.");
        }
Esempio n. 8
0
        private void button2_Click(object sender, EventArgs e)
        {
            mainBoard.ComputerVsComputer();

            DrawBoard();
            if (mainBoard.CheckGameEnd())
            {
                MessageBox.Show("Игра завершена\n" + mainBoard.GetWinner());
                mainBoard = null;
            }
        }
Esempio n. 9
0
        static void ModePlayer1VsComp(TicTacToeBoard board)
        {
            do
            {
                Console.WriteLine("\nFirst to play Player 1");
                char prime = board.Chip1;
                Console.WriteLine("~~~~~~~~~~~The game started~~~~~~~~~~~");
                while (!board.IsFullBoard)
                {
                    board.showBoard();
                    Console.WriteLine($"Puts the chip player \'{prime}\':");
                    Console.WriteLine($"Select the position (0 - 8) where to insert the chip: ");
                    int pos;
                    while (!int.TryParse(Console.ReadLine(), out pos))
                    {
                        Console.WriteLine("This is not a number!. Follow the instructions!");
                    }
                    board.changeBoard(prime, pos);
                    board.showBoard();
                    if (board.IsWinner(prime))
                    {
                        board.showBoard();
                        break;
                    }

                    if (board.IsFullBoard)
                    {
                        Console.WriteLine("The board is full.");
                        break;
                    }

                    Console.WriteLine("The computer puts its chip");
                    int compM = board.CompMove();
                    Console.WriteLine($"CompMove = {compM}");
                    board.changeBoard(board.ChipComp, compM);
                    if (board.IsWinner(board.ChipComp))
                    {
                        board.showBoard();
                        break;
                    }

                    prime = board.Chip1;

                    if (board.IsFullBoard)
                    {
                        Console.WriteLine("The board is full. No changes");
                        board.showBoard();
                    }
                }
            } while (QuestionOnRepeat(board));
        }
Esempio n. 10
0
 private void UpdateBoard(int row, int col)
 {
     if (mainBoard.IsMoveAllowed(row, col))
     {
         string[,] board = mainBoard.GetUpdatedBoard(row, col);
         DrawBoard();
         if (mainBoard.CheckGameEnd())
         {
             MessageBox.Show("Игра завершена\n" + mainBoard.GetWinner());
             mainBoard = null;
         }
     }
     else
     {
         MessageBox.Show("Недопустимый ход");
     }
 }
Esempio n. 11
0
 public Form1()
 {
     InitializeComponent();
     mainBoard           = null;
     pictureBox1.Visible = false;
     pictureBox2.Visible = false;
     pictureBox3.Visible = false;
     pictureBox4.Visible = false;
     pictureBox5.Visible = false;
     pictureBox6.Visible = false;
     pictureBox7.Visible = false;
     pictureBox8.Visible = false;
     pictureBox9.Visible = false;
     groupBox2.Visible   = false;
     button1.Enabled     = false;
     button2.Visible     = false;
 }
        public UltimateWindow(string gameMode)
        {
            mode = gameMode;
            InitializeComponent();
            gameBoard = new TicTacToeBoard(9, "Normal Tic-tac-toe");

            theBoards       = new TicTacToeBoard[3, 3];
            theBoards[0, 0] = new TicTacToeBoard(3, "Normal Tic-tac-toe");
            theBoards[0, 1] = new TicTacToeBoard(3, "Normal Tic-tac-toe");
            theBoards[0, 2] = new TicTacToeBoard(3, "Normal Tic-tac-toe");
            theBoards[1, 0] = new TicTacToeBoard(3, "Normal Tic-tac-toe");
            theBoards[1, 1] = new TicTacToeBoard(3, "Normal Tic-tac-toe");
            theBoards[1, 2] = new TicTacToeBoard(3, "Normal Tic-tac-toe");
            theBoards[2, 0] = new TicTacToeBoard(3, "Normal Tic-tac-toe");
            theBoards[2, 1] = new TicTacToeBoard(3, "Normal Tic-tac-toe");
            theBoards[2, 2] = new TicTacToeBoard(3, "Normal Tic-tac-toe");
            // MessageBox.Show("Player X is going first.");
        }
Esempio n. 13
0
        // Метод для обработки события записи на диск результатов
        public static void ResultToFile(object sender, TicTacToeEventArgs ev)
        {
            Console.WriteLine(ev.Massage);
            TicTacToeBoard newBoard = sender as TicTacToeBoard;

            DriveInfo[] drives  = DriveInfo.GetDrives();
            string      dirName = drives[0].Name;

            if (drives[0].AvailableFreeSpace > 10000 && drives[0].IsReady)
            {
                var    dir  = Directory.CreateDirectory(dirName + "ResultOfTic-Tac-Toe");
                string path = dir.ToString() + "\\result.txt";

                string result = $"Побед компьютера: {ev.CountComp}\nПобед игрока: {ev.CountPlayer}";

                File.WriteAllText(path, result);
            }
        }
Esempio n. 14
0
        static bool QuestionOnRepeat(TicTacToeBoard board)
        {
            string answer;

            Console.WriteLine("Want to play one more time?(y/n)");
            answer = Console.ReadLine();

            if (answer.ToLower().Equals("y"))
            {
                board.ResetBoard();
                board.ClearListPositionsAndCharArray();
                board.IsFullBoard = false;
                return(true);
            }
            else
            {
                Console.WriteLine("This is NO!");
            }
            return(false);
        }
Esempio n. 15
0
        public ThreeDWindow(string gameMode, TicTacToeBoard boardFromFile) : this(gameMode)
        {
            mode      = gameMode;
            gameBoard = boardFromFile;
            InitializeComponent();

            Button btn;
            if (gameBoard.ThreeDGrid[0, 0, 0].symbol != '\0')
            {
                btn         = _a0;
                btn.Content = gameBoard.ThreeDGrid[0, 0, 0].symbol;
            }
            if (gameBoard.ThreeDGrid[1, 0, 0].symbol != '\0')
            {
                btn         = _b0;
                btn.Content = gameBoard.ThreeDGrid[1, 0, 0].symbol;
            }
            if (gameBoard.ThreeDGrid[2, 0, 0].symbol != '\0')
            {
                btn         = _c0;
                btn.Content = gameBoard.ThreeDGrid[2, 0, 0].symbol;
            }
            if (gameBoard.ThreeDGrid[0, 1, 0].symbol != '\0')
            {
                btn         = _d0;
                btn.Content = gameBoard.ThreeDGrid[0, 1, 0].symbol;
            }
            if (gameBoard.ThreeDGrid[1, 1, 0].symbol != '\0')
            {
                btn         = _e0;
                btn.Content = gameBoard.ThreeDGrid[1, 1, 0].symbol;
            }
            if (gameBoard.ThreeDGrid[2, 1, 0].symbol != '\0')
            {
                btn         = _f0;
                btn.Content = gameBoard.ThreeDGrid[2, 1, 0].symbol;
            }
            if (gameBoard.ThreeDGrid[0, 2, 0].symbol != '\0')
            {
                btn         = _g0;
                btn.Content = gameBoard.ThreeDGrid[0, 2, 0].symbol;
            }
            if (gameBoard.ThreeDGrid[1, 2, 0].symbol != '\0')
            {
                btn         = _h0;
                btn.Content = gameBoard.ThreeDGrid[1, 2, 0].symbol;
            }
            if (gameBoard.ThreeDGrid[2, 2, 0].symbol != '\0')
            {
                btn         = _i0;
                btn.Content = gameBoard.ThreeDGrid[2, 2, 0].symbol;
            }
            if (gameBoard.ThreeDGrid[0, 0, 1].symbol != '\0')
            {
                btn         = _a1;
                btn.Content = gameBoard.ThreeDGrid[0, 0, 1].symbol;
            }
            if (gameBoard.ThreeDGrid[1, 0, 1].symbol != '\0')
            {
                btn         = _b1;
                btn.Content = gameBoard.ThreeDGrid[1, 0, 1].symbol;
            }
            if (gameBoard.ThreeDGrid[2, 0, 1].symbol != '\0')
            {
                btn         = _c1;
                btn.Content = gameBoard.ThreeDGrid[2, 0, 1].symbol;
            }
            if (gameBoard.ThreeDGrid[0, 1, 1].symbol != '\0')
            {
                btn         = _d1;
                btn.Content = gameBoard.ThreeDGrid[0, 1, 1].symbol;
            }
            if (gameBoard.ThreeDGrid[1, 1, 1].symbol != '\0')
            {
                btn         = _e1;
                btn.Content = gameBoard.ThreeDGrid[1, 1, 1].symbol;
            }
            if (gameBoard.ThreeDGrid[2, 1, 1].symbol != '\0')
            {
                btn         = _f1;
                btn.Content = gameBoard.ThreeDGrid[2, 1, 1].symbol;
            }
            if (gameBoard.ThreeDGrid[0, 2, 1].symbol != '\0')
            {
                btn         = _g1;
                btn.Content = gameBoard.ThreeDGrid[0, 2, 1].symbol;
            }
            if (gameBoard.ThreeDGrid[1, 2, 1].symbol != '\0')
            {
                btn         = _h1;
                btn.Content = gameBoard.ThreeDGrid[1, 2, 1].symbol;
            }
            if (gameBoard.ThreeDGrid[2, 2, 1].symbol != '\0')
            {
                btn         = _i1;
                btn.Content = gameBoard.ThreeDGrid[2, 2, 1].symbol;
            }
            if (gameBoard.ThreeDGrid[0, 0, 2].symbol != '\0')
            {
                btn         = _a2;
                btn.Content = gameBoard.ThreeDGrid[0, 0, 2].symbol;
            }
            if (gameBoard.ThreeDGrid[1, 0, 2].symbol != '\0')
            {
                btn         = _b2;
                btn.Content = gameBoard.ThreeDGrid[1, 0, 2].symbol;
            }
            if (gameBoard.ThreeDGrid[2, 0, 2].symbol != '\0')
            {
                btn         = _c2;
                btn.Content = gameBoard.ThreeDGrid[2, 0, 2].symbol;
            }
            if (gameBoard.ThreeDGrid[0, 1, 2].symbol != '\0')
            {
                btn         = _d2;
                btn.Content = gameBoard.ThreeDGrid[0, 1, 2].symbol;
            }
            if (gameBoard.ThreeDGrid[1, 1, 2].symbol != '\0')
            {
                btn         = _e2;
                btn.Content = gameBoard.ThreeDGrid[1, 1, 2].symbol;
            }
            if (gameBoard.ThreeDGrid[2, 1, 2].symbol != '\0')
            {
                btn         = _f2;
                btn.Content = gameBoard.ThreeDGrid[2, 1, 2].symbol;
            }
            if (gameBoard.ThreeDGrid[0, 2, 2].symbol != '\0')
            {
                btn         = _g2;
                btn.Content = gameBoard.ThreeDGrid[0, 2, 2].symbol;
            }
            if (gameBoard.ThreeDGrid[1, 2, 2].symbol != '\0')
            {
                btn         = _g2;
                btn.Content = gameBoard.ThreeDGrid[1, 2, 2].symbol;
            }
            if (gameBoard.ThreeDGrid[2, 2, 2].symbol != '\0')
            {
                btn         = _i2;
                btn.Content = gameBoard.ThreeDGrid[2, 2, 2].symbol;
            }
        }
 public CustomWindow(string gameMode)
 {
     mode = gameMode;
     InitializeComponent();
     gameBoard = new TicTacToeBoard((int)Application.Current.Properties["BoardSize"], mode);
 }
 public UltimateWindow(string gameMode, TicTacToeBoard boardFromFile) : this(gameMode)
 {
     this.boardFromFile = boardFromFile;
     mode = gameMode;
     InitializeComponent();
 }
Esempio n. 18
0
        static void ModePlayer1VsPlayer2(TicTacToeBoard board)
        {
            do
            {
                Console.WriteLine("Determine who goes first: press your character or press \'r\' and it will be chosen randomly:");

                char prime;
                while (!char.TryParse(Console.ReadLine(), out prime) && !char.IsLetter(prime))
                {
                    Console.WriteLine("Invalid character! Follow the instructions.");
                }

                if (prime == board.Chip1)
                {
                    prime = board.Chip1;
                }
                else if (prime == board.Chip2)
                {
                    prime = board.Chip2;
                }
                else if (prime == 'r')
                {
                    Random rand = new Random();
                    int    r    = rand.Next(1, 1000);
                    if (r % 2 == 0)
                    {
                        prime = board.Chip1;
                    }
                    else
                    {
                        prime = board.Chip2;
                    }
                }
                else
                {
                    Console.WriteLine($"Such a chip does not exist! Default state, first: {board.Chip1}");
                    prime = board.Chip1;
                }

                Console.WriteLine("~~~~~~~~~~~The game started~~~~~~~~~~~");
                while (!board.IsFullBoard)
                {
                    board.showBoard();
                    Console.WriteLine($"Puts the chip player \'{prime}\':");
                    Console.WriteLine($"Select the position (0 - 8) where to insert the chip: ");
                    int pos;
                    while (!int.TryParse(Console.ReadLine(), out pos))
                    {
                        Console.WriteLine("This is not a number!. Follow the instructions!");
                    }
                    board.changeBoard(prime, pos);
                    if (board.IsWinner(prime))
                    {
                        board.showBoard();
                        break;
                    }

                    if (prime == board.Chip1)
                    {
                        prime = board.Chip2;
                    }
                    else
                    {
                        prime = board.Chip1;
                    }
                }

                if (board.IsFullBoard)
                {
                    Console.WriteLine("The board is full. No changes");
                    board.showBoard();
                }
            } while (QuestionOnRepeat(board));

            Console.WriteLine("Goodbye!!!");
            Console.ReadLine();
        }