public static char[,] MakeMove(char[,] boardMatrix, int size) { // loop through all board positions until valid move is found for (int xcoord = 0; xcoord < size; xcoord++) { for (int ycoord = 0; ycoord < size; ycoord++) { // look left for (int x = xcoord; x > 0; x--) { // look all positions to left for friendly char if (boardMatrix[x, ycoord] == 'O') { // look 1 position to left for opposing char if (boardMatrix[xcoord - 1, ycoord] == 'X') { int xstop = x; // change positions for (int j = xcoord; j > xstop; j--) { boardMatrix[j, ycoord] = 'O'; } Console.WriteLine("Computer entered a move at {0}{1}", xcoord, ycoord); return(boardMatrix); } } } // look right for (int x = xcoord; x < Math.Sqrt(boardMatrix.Length); x++) { // look all positions to the right for friendly char if (boardMatrix[x, ycoord] == 'O') { // look 1 position to the right for opposing char if (boardMatrix[xcoord + 1, ycoord] == 'X') { int xstop = x; // change positions for (int j = xcoord; j < xstop; j++) { boardMatrix[j, ycoord] = 'O'; } Console.WriteLine("Computer entered a move at {0}{1}", xcoord, ycoord); return(boardMatrix); } } } // look up for (int y = ycoord; y >= 0; y--) { // look all positions up for friendly char if (boardMatrix[xcoord, y] == 'O') { // look 1 position up for opposing char if (boardMatrix[xcoord, ycoord - 1] == 'X') { int ystop = y; // change positions for (int j = ycoord; j > ystop; j--) { boardMatrix[xcoord, j] = 'O'; } Console.WriteLine("Computer entered a move at {0}{1}", xcoord, ycoord); return(boardMatrix); } } } // look down for (int y = ycoord; y < Math.Sqrt(boardMatrix.Length); y++) { // look all positions below for friendly char if (boardMatrix[xcoord, y] == 'O') { // look 1 position below for opposing char if (boardMatrix[xcoord, ycoord + 1] == 'X') { int ystop = y; // change positions for (int j = ycoord; j < ystop; j++) { boardMatrix[xcoord, j] = 'O'; } Console.WriteLine("Computer entered a move at {0}{1}", xcoord, ycoord); return(boardMatrix); } } } } } // else end game EndOfGame.Score(boardMatrix); return(boardMatrix); }
public static void TurnLoop(string mode, int size, char[,] boardMatrix) { // set player char arrays for looping though char[] activeplayer = new char[] { 'X', 'O' }; char[] idleplayer = new char[] { 'O', 'X' }; int[] playerid = new int[] { 1, 2 }; bool gameover = false; // loop for 2 human players if (mode == "h1") { while (gameover == false) { for (int i = 0; i < 2; i++) { // render board map Board.DrawMap(size); // render in board state Console.WriteLine("\n"); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { Console.Write(string.Format("|{0}", boardMatrix[x, y])); if (x == size - 1) { Console.Write("|\n"); } } } // prompt and handle user input Console.WriteLine("\nPlayer {0}, your pieces are denoted by '{1}'. Enter a 2 digit co-ordinate to place, 'help' to view rules again, 'save' to save the current board state and 'end' to view the current score and finish:", playerid[i], activeplayer[i]); string req = Console.ReadLine(); bool input = Int32.TryParse(req, out int placing); // print game rules if (req == "help") { Help.PrintHelp(size); } // save board matrix to txt file else if (req == "save") { using (StreamWriter sr = new StreamWriter("save.txt")) { foreach (var item in boardMatrix) { sr.WriteLine(item); } } Console.WriteLine("The game has been saved to save.txt."); } // end game - display score else if (req == "end") { EndOfGame.Score(boardMatrix); } // action move request if valid number input else if (input) { int xcoord = placing / 10; int ycoord = placing % 10; boardMatrix = HumanMove.LookAndPlace(xcoord, ycoord, boardMatrix, activeplayer[i], idleplayer[i]); // end of game test int count = 0; foreach (char square in boardMatrix) { if (square == ' ') { count++; } } if (count == 0) { EndOfGame.Score(boardMatrix); } } else { Console.Write("\nInput not recognised."); } } } } // loop for 1 computer player, 1 human else if (mode == "c1") { while (gameover == false) { for (int i = 0; i < 2; i++) { // render in board state console Console.WriteLine("\n"); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { Console.Write(string.Format("|{0}", boardMatrix[x, y])); if (x == size - 1) { Console.Write("|\n"); } } } // prompt and handle human input Console.WriteLine("\nPlayer 1, your pieces are denoted by 'X'. Enter a 2 digit co-ordinate to place, 'help' to view rules again, 'save' to save the current board state and 'end' to view the current score and finish:"); string req = Console.ReadLine(); bool input = Int32.TryParse(req, out int placing); // print game rules if (req == "help") { Help.PrintHelp(size); } // save board state to txt file else if (req == "save") { using (StreamWriter sr = new StreamWriter("save.txt")) { foreach (var item in boardMatrix) { sr.WriteLine(item); } } Console.WriteLine("The game has been saved to save.txt."); } // end game - show scores else if (req == "end") { EndOfGame.Score(boardMatrix); } // action move request if valid number input else if (input) { int xcoord = placing / 10; int ycoord = placing % 10; boardMatrix = HumanMove.LookAndPlace(xcoord, ycoord, boardMatrix, 'X', 'O'); } else { Console.Write("\nInput not recognised."); } // computer move boardMatrix = EasyAI.MakeMove(boardMatrix, size); // test for full board, if so end game foreach (char square in boardMatrix) { if (square == ' ') { break; } else { EndOfGame.Score(boardMatrix); } } } } } }