I'm creating a TicTacToe class that contains a 3 by 3 rectangular array of integers & is played by 2 human players. "1" is used for the first player's move & "2" is used for the second player's move. Currently, I am stuck and have no clue on how to determine/check whether the game has been won or whether its a draw after each and every move has been made.
Unfortunately, I'm stuck in checking if the game is won or drawn after each player makes a move & was hoping someone could help.
This is my code thus far: Main class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TicTacToe game = new TicTacToe();
game.PrintBoard();
game.Play();
}
}
}
TicTacToe class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class TicTacToe
{
private const int BOARDSIZE = 3; //size of the board
private int[,] board = new int [BOARDSIZE,BOARDSIZE]; // board representation
string player1row, player1column, player2row, player2column;
enum DONE {win1, win2, win3, win4, win5, win6, win7,win8,
win9, win10, win11, win12, win13, win14, win15, win16};
//Default Constructor
public TicTacToe(){
board = new int [3,3] { {0,0,0},{0,0,0},{0,0,0} };
}
int win1 = 1;
public void PrintBoard(){
Console.WriteLine("-----------------------\n"+
"| | | |\n"+
"| {0} | {1} | {2} |\n" +
"|_______|_______|_______|\n"+
"| | | |\n"+
"| {3} | {4} | {5} |\n" +
"|_______|_______|_______|\n"+
"| | | |\n"+
"| {6} | {7} | {8} |\n" +
"|_______|_______|_______|\n",
board[0,0],board[0,1],board[0,2],
board[1,0],board[1,1],board[1,2],
board[2,0],board[2,1],board[2,2]);
}// end PrintBoard method
public void Play(){
while (true)
{
Console.WriteLine("Player 1's turn.");
Console.Write("Player 1: Enter row ( 0 <= row < 3 ): "); //prompt user
player1row = Console.ReadLine(); //get string from user
Console.Write("Player 1: Enter column ( 0 <= row < 3 ): "); //prompt user
player1column = Console.ReadLine(); //get string from user
//Convert string to ints
int p1r = Convert.ToInt32(player1row);
int p1c = Convert.ToInt32(player1column);
// assign marker to desired position
board[p1r, p1c] = 1;
PrintBoard(); // Update board
checkWinner();
Console.WriteLine("\nPlayer 2's turn.");
Console.Write("Player 2: Enter row ( 0 <= row < 3 ): ");
player2row = Console.ReadLine(); //get string from user
Console.Write("Player 2: Enter column ( 0 <= row < 3 ): ");
player2column = Console.ReadLine(); //get string from user
//Convert string to ints
int p2r = Convert.ToInt32(player2row);
int p2c = Convert.ToInt32(player2column);
// assign marker to desired position
board[p2r, p2c] = 2;
PrintBoard(); // Update board
checkWinner();
}
}//end Play method
private bool checkWinner()
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
//I WANT TO CHECK FOR A WIN OR DRAW HERE BUT DONT KNOW HOW TO
}}// end class
I want to check for a winner using 2 for loops, the first checking the rows, then another checking for columns, then adding 2 separate if statements to check the diagonals, but i really don't know how to & have been stuck for a while now.
I wont code it for you but here is the logic you can use:
1) For each column check if all rows are the same, if yes declare winner. If not go to step 2.
2) For each row check if all column values are the same, if yes declare winner. If not go to step 3.
3) Now check diagonals, there are only two possibilities here [0,0] [1,1] [2,2] and also [0,2] [1,1] [2,0] if they are the same declare winner, if not check whether all values in array are filled, if yes declare draw if not make users enter values.
As Iiya has said, you're going to have to through through all the win cases. There's many ways to do that but the very gist of it in code would be
private bool checkWinner(int player)
{
// check rows
if (board[0, 0] == player && board[0, 1] == player && board[0, 2] == player) { return true; }
if (board[1, 0] == player && board[1, 1] == player && board[1, 2] == player) { return true; }
if (board[2, 0] == player && board[2, 1] == player && board[2, 2] == player) { return true; }
// check columns
if (board[0, 0] == player && board[1, 0] == player && board[2, 0] == player) { return true; }
if (board[0, 1] == player && board[1, 1] == player && board[2, 1] == player) { return true; }
if (board[0, 2] == player && board[1, 2] == player && board[2, 2] == player) { return true; }
// check diags
if (board[0, 0] == player && board[1, 1] == player && board[2, 2] == player) { return true; }
if (board[0, 2] == player && board[1, 1] == player && board[2, 0] == player) { return true; }
return false;
}
You can optimise this however which way you'd like (using for loops or matrices). Note the checkWinner() function requires a player input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With