using System; using System.Collections.Generic; namespace TicTacToe_Game { class TicTacToe : TicTacToe_base { private readonly String player = "X"; private readonly String computer = "O"; private bool playerTurn; public TicTacToe() : base() {} // checks if the user or the computer has won the game protected override bool CheckWin(String user) { foreach (int[] winList in WinLists) { if (board.ContainsKey(winList[0]) && board[winList[0]] == user && board.ContainsKey(winList[1]) && board[winList[1]] == user && board.ContainsKey(winList[2]) && board[winList[2]] == user) return true; } return false; } // runs the game public override void Run() { Console.WriteLine("Welcome to Tic-Tac-Toe!"); DrawBoard(); playerTurn = true; while (true) { int pos = 0; if (playerTurn) { Console.WriteLine("Enter a position (1-9): "); try { pos = Convert.ToInt32(Console.ReadLine()); } catch (FormatException) { Console.WriteLine("Invalid input. Please enter a number."); continue; } if (!AddInputToBoard(pos, player)) { Console.WriteLine("Invalid input. Please enter a valid position."); continue; } } else { while (!AddInputToBoard(pos, computer)) { pos = FindNextPosByAI(); } Console.WriteLine("Computer placed an O."); } DrawBoard(); if (CheckWin(player)) { Console.WriteLine("You win!"); break; } else if (CheckWin(computer)) { Console.WriteLine("Computer wins!"); break; } else if (board.Count == 9) { Console.WriteLine("Tie game!"); break; } playerTurn = !playerTurn; } } } }