I've been working on a side project of Tic Tac Toe, it works fine, but I'd like to change the winner function to be dynamic and work for any size board.

For example, say I want a 4x3 or a 10x10 board how could I make the one function work for any board size? The board size is changed by changing the values of the global variables BOARD_ROW_NUM and BOARD_COL_NUM.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

const int BOARD_ROW_NUM=3;
const int BOARD_COL_NUM=3;
const int EMPTY = -1;
const int PLAYER1 = 1;
const int PLAYER2 = 2;
const char SYMBOL1 = 'X';
const char SYMBOL2 = 'O';


void initializeboard(int board[BOARD_ROW_NUM][BOARD_COL_NUM]) //initialize the board in a 2d array
{
	for(int i=0; i<BOARD_ROW_NUM; i++)
	{
		for(int j=0; j<BOARD_COL_NUM; j++)
		{
			board[i][j]=EMPTY;
		}
	}

} //END INTIIALIZE BOARD


void playerturn(int &turn, int PLAYER1, int PLAYER2)
{
	if(turn==PLAYER1)
		turn=PLAYER2;
	else
		turn=PLAYER1;

} //END PLAYER TURN



void playerinput(int board[][BOARD_COL_NUM], int BOARD_ROW_NUM, char SYMBOL1, char SYMBOL2, int &row, int &col, int turn)
{	
	cout<<"NOTE: the first position has coordinates (0 0)"<<endl;
	cout<<"Player"<<turn<<" please input your desired location (row col)"<<endl;
	cin>>row>>col;

	if(board[row][col]=='X' || board[row][col]=='O')
	{
		cout<<"**Invalid input, try again**"<<endl;
		playerinput(board, BOARD_ROW_NUM, SYMBOL1, SYMBOL2, row, col, turn);
	}

	if(row != 0 && row != 1 && row != 2)
	{
		cout<<"**Invalid input, try again**"<<endl;
		playerinput(board, BOARD_ROW_NUM, SYMBOL1, SYMBOL2, row, col, turn);
	}

}


void printboard(int board[][BOARD_COL_NUM], int BOARD_ROW_NUM)
{
	cout<<"+-+-+-+"<<endl;

	for(int i=0; i<BOARD_ROW_NUM; i++)
	{
		cout<<"|";

		for(int j=0; j<BOARD_COL_NUM; j++)
		{
			if(board[i][j]==PLAYER1)cout<<"X|";
			else if(board[i][j]==PLAYER2)cout<<"O|";
			else
				cout<<" "<<"|";
		}

		cout<<endl;

		cout<<"+-+-+-+"<<endl;
	}

} //END PRINT BOARD



int winner (int board[][BOARD_COL_NUM], int BOARD_ROW_NUM, bool &gameover, bool &gamewon, int turn, int step)
{

	if (step<3)gameover=false;

	if (board[0][0] != ' ') 
	{
		if (board[0][1] == board[0][0] && board[0][2] == board[0][0])
		{
			gameover = true;
			gamewon = true;
		}

		if (board[1][0] == board[0][0] && board[2][0] == board[0][0])
		{
			gameover = true;
			gamewon = true;
		}
	}

	if (board[1][1] != ' ') 
	{
		if (board[0][0] == board[1][1] && board[2][2] == board[1][1])
		{
			gameover = true;
			gamewon = true;
		}

		if (board[0][1] == board[1][1] && board[2][1] == board[1][1])
		{
			gameover = true;
			gamewon = true;
		}
				
		if (board[1][0] == board[1][1] && board[1][2] == board[1][1])
		{
			gameover = true;
			gamewon = true;
		}

		if (board[0][2] == board[1][1] && board[2][0] == board[1][1])
		{
			gameover = true;
			gamewon = true;
		}
		
	}

	if (board[2][2] != ' ') 
	{
		if (board[0][2] == board[2][2] && board[1][2] == board[2][2])
		{
			gameover = true;
			gamewon = true;
		}
	
		if (board[2][0] == board[2][2] && board[2][1] == board[2][2]) 
		{
			gameover = true;
			gamewon = true;
		}
				
	}

	// Check for a draw
	if (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] != ' ' && step==9)
	{
		gameover = true;
		gamewon = false;
	}

	//Display Win/Draw Message
	if (gameover == true) 
	{
		if (gamewon==false)
		{
			cout << "Tie game!" <<endl;
			cout << "\nGame Over." <<"\n";
		}

		if (gamewon==true)
		{
			cout << "\nCongratulations Player " <<turn<< ". You won!" << endl;
			cout << "\nGame Over." <<"\n";
		}

		return 1;
	}

} //END WINNER

int main()
{

	int board[BOARD_ROW_NUM][BOARD_COL_NUM];
	int row, col;
	int turn; //player1 or player2
	int step=0;

	bool gamewon=false;
	bool gameover=false;

	turn=PLAYER1;

	ofstream log_file;

	log_file.open ("moves.txt");
	if (log_file.fail())
 	{
		cout <<"Failed to open file moves.txt to write\n";
		return 1;
	}

	do
	{
		printboard(board, BOARD_ROW_NUM);//print the board

		playerinput(board, BOARD_ROW_NUM, SYMBOL1, SYMBOL2, row, col, turn); //get player input

		log_file << "Player" << turn << " " << row << "  " << col <<endl;

		board[row][col]=turn; step++; //update the board

		winner(board, BOARD_ROW_NUM, gameover, gamewon, turn, step); //check for winner

		playerturn(turn, PLAYER1, PLAYER2); //switch players

	} while(step<BOARD_ROW_NUM*BOARD_COL_NUM && gameover != true);

	 log_file.close();

} //END MAIN

Recommended Answers

All 2 Replies

first off how would the game work on say a 4x4 board?
how would a diagonal win work?(is it still 3 in a row or is it the full length of the board) if its the former its closer to a connect 4 game. if its the latter then again how would a diagonal work?

Sorry for taking so long, been busy. Yes, it still looks for 3 in a row. Here's an idea I came up with, but I'm having trouble making it work, any ideas? EDIT: this is for the winner function.

//pass row and col
	//row+1/-1 etc
	//make sure you dont leave the array

	if (step<3)gameover=false;

	if(col <= BOARD_COL_NUM-2)
	{
		if(board[row][col] == board[row][col+1] && board[row][col]==board[row][col+2])
		{
			gameover = true;
			gamewon = true;
		}
		
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.