Do any one have the source codes for a Naughts & Crosses Game

Do any one have the source codes for a Naughts & Crosses Game?
If you have please send it as an email to henryjia18@yahoo.com or add it here as a comment.
Thankyou
A) Platform?
B) Console / GUI?
C) With or without advanced graphic libraries?
D) What do you need it for?
A) Windows
B) Console
C) Without advanced graphic libraries
Last edited on
What? No. Do your own homework.

It isn't a difficult problem. Heck, it's not even an easy problem. It's simpler than that.
For God's sake, I've only been learning C++ for 6 months!
Um... sorry to hear. However, as Duoas correctly stated, the problem shouldn't be that hard.

If you have a piece of code you'd like us to review, we'll happily do that, but we try to avoid giving out code or step-by-step solutions (for good reasons too).

-Albatross
If you have been learning C++ for a few months already, this should really be no problem anymore for you... Naughts & Crosses is something you normally do in your first weeks... Where exactly is your problem?
But how do you get the computer to know if the player has put 2 crosses in a row? That's the bit of source code that I really need!
Think about using a two dimensional array to represent the grid. That should give you some idea of how to implement the conditions for winning.

EDIT: PS - I just noticed you put your email address in your post. You know that that can get you spam, right?
Last edited on
You will have to use a few functions for each thing your program does. One function to get a user's input, another to display the gameboard, another to check for a win, etc. You'll also have to keep track of whose turn it is, X or O.

BTW, you don't have to check for two in a row, just check for three in a row. That's fairly straightforward:
X| | 	 |X| 	 | |X
-+-+-	-+-+-	-+-+-
X| | 	 |X| 	 | |X
-+-+-	-+-+-	-+-+-
X| | 	 |X| 	 | |X

X|X|X	 | | 	 | | 
-+-+-	-+-+-	-+-+-
 | | 	X|X|X	 | | 
-+-+-	-+-+-	-+-+-
 | | 	 | | 	X|X|X

X| | 	 | |X
-+-+-	-+-+-
 |X| 	 |X| 
-+-+-	-+-+-
 | |X	X| | 

You only need a 3 by 3 array of characters for your gameboard.

Hope this helps.
I think he wanted to know if there are two in a row so that the computer could act accordingly and block the user from getting three in a row on the next move.

That's also fairly straightforward, it's just that the easiest way out would require you to do more checks (think: 8 three-in-a-rows * 3 possibilities for the removal of one naught or cross per three-in-a-row = 24 possibilities).

-Albatross
Last edited on
That's exactly what I mean Albatross
So have you worked out how to do it now, or do you still need help with it?
Yes because if the situation of the game end up as something like this:

  |X|   
 X|X|O
 O| |


the game gets sort of jammed because the computer misses a go it's self for some reason.

Here's the source code:


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <conio.h>

char matrix[3][3];
char check(void);
void init_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);
using namespace std;
int main(int argc, char **argv)
{
	for(;;){
	char done;
	printf("This is the game of Tic Tac Toe.\n");
	printf("You will be playing against the computer.\n");
	
	done=' ';
	
	init_matrix();
	do{
		disp_matrix();
		get_player_move();
		done = check();
		if(done!=' ') break;
		get_computer_move();
		done = check();
	}while(done==' ');
	
	if(done=='X') printf("You won!\n");
	else printf("I won!!!!\n");
	disp_matrix();
	char anything;
	cout<<"\nPress anything to continue";
	anything=getch();
	}
	return 0;
}
void init_matrix(void)
{
	int i,j;
	for(i=0;i<3;i++)
		for(j=0;j<3;j++) matrix[i][j] = ' ';
}
void get_player_move(void)
{
	int x,y;
	printf("\nEnter coordinates for your X: ");
	scanf("%d,%d",&y,&x);
	x--; y--;
	if(matrix[x][y]!=' '){
		printf("Invalid move,try again.\n");
		get_player_move();
	}
	else matrix[x][y] = 'X';
}
void get_computer_move(void)
{
	for(int i=0;i<3;i++)
	{
		//rows
		if(matrix[i][0]==matrix[i][1]&&matrix[i][1]=='X')
		{
			matrix[i][2]='o';
			return;
		}
		if(matrix[i][1]==matrix[i][2]&&matrix[i][1]=='X')
		{
			matrix[i][0]='o';
			return;
		}
		if(matrix[i][0]==matrix[i][2]&&matrix[i][0]=='X')
		{
			matrix[i][1]='o';
			return;
		}
		//collumns
		if(matrix[0][i]==matrix[1][i]&&matrix[1][i]=='X')
		{
			matrix[2][i]='o';
			return;
		}
		if(matrix[1][i]==matrix[2][i]&&matrix[1][i]=='X')
		{
			matrix[0][i]='o';
			return;
		}
		if(matrix[0][i]==matrix[2][i]&&matrix[0][i]=='X')
		{
			matrix[1][i]='o';
			return;
		}
	}
	if(matrix[0][0]==matrix[1][1]&&matrix[1][1]=='X')
	{
		matrix[2][2]='o';
		return;
	}
	if(matrix[1][1]==matrix[2][2]&&matrix[1][1]=='X')
	{
		matrix[0][0]='o';
		return;
	}
	if(matrix[0][0]==matrix[2][2]&&matrix[0][0]=='X')
	{
		matrix[1][1]='o';
		return;
	}
	if(matrix[0][2]==matrix[1][1]&&matrix[1][1]=='X')
	{
		matrix[2][0]='o';
		return;
	}
	if(matrix[1][1]==matrix[2][0]&&matrix[1][1]=='X')
	{
		matrix[2][0]='o';
		return;
	}
	if(matrix[2][0]==matrix[0][2]&&matrix[0][2]=='X')
	{
		matrix[1][1]='o';
		return;
	}
	int i,j;
	for(i=0;i<3;i++)
	{
		for(j=0;i<3;i++)
		{
			if(i*j==9)
			{
				printf("draw\n");
				char anything;
				cout<<"\nPress anything to continue";
				anything=getch();
				exit(0);
			}
			else
			{
				if(matrix[i][j]==' ')
				{
					matrix[i][j]='o';
					break;
				}
			}
		}
		if(matrix[i][j]=='o')
			break;
	}
}
void disp_matrix(void)
{
	int t;
	for(t=2;t>=0;t--){
		printf(" %c | %c | %c ",matrix[t][0],matrix[t][1],matrix[t][2]);
		if(t!=0) printf("\n---|---|---\n");
	}
}
char check(void)
{
	int i;
	
	for(i=0;i<3;i++)
		if(matrix[i][0]==matrix[i][1] && matrix[i][0]==matrix[i][2]) 
			return matrix[i][0];
	for(i=0;i<3;i++)
		if(matrix[0][i]==matrix[1][i] && matrix[0][i]==matrix[2][i]) 
			return matrix[0][i];
	if(matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) 
			return matrix[i][0];
	if(matrix[0][2]==matrix[1][1] && matrix[1][1]==matrix[2][0]) 
			return matrix[i][0];
	return ' ';
}
Last edited on
Consider the case you displayed:
  |X|   
 X|X|O
 O| |

There are two possibilities: the horizontal and the vertical.

Your code does not check all possible cases -- it finds the horizontal case and aborts, assuming that there is not other possible case.

Reconsider the code this way: you want to look for any potential three in a row. That is, it should ask the following questions, in order:
1. Is it possible for me to place my piece and have a three in a row right now? (If yes, do it.)
2. Is it possible for my opponent to place a piece and have a three in a row right now? (If yes, place my piece there.)

There are other questions you can ask if the answer to both is "no", but I'll leave it at that.

Hope this helps.
It does check every possibilities and works when the 3 by 3 square is not almost filled up but as soon as the 3 by 3 square is almost filled up, it jamms.
Last edited on
If it is jamming then perhaps it gets stuck in a loop somewhere. Try setting breakpoints at various places and stepping through it with a debugger to find out where in the code your problem is.
Not that sort of jam, jam as in that the program does not do somthing it should do.
I found out how to get the program working now. There was aproblem because I forgot to get it to check if there was an empty space or not so it kept on putting an 'O' where there's already an 'O'. Thanks for the advices guys!!!
Topic archived. No new replies allowed.