Click here to Skip to main content
15,900,973 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: CFileDialog, just thought I should share this Pin
PJ Arends7-Sep-03 8:34
professionalPJ Arends7-Sep-03 8:34 
GeneralEditable ListBox Pin
ben26-Sep-03 18:38
ben26-Sep-03 18:38 
GeneralRe: Editable ListBox Pin
Neville Franks6-Sep-03 22:22
Neville Franks6-Sep-03 22:22 
GeneralRe: Editable ListBox Pin
ben26-Sep-03 22:52
ben26-Sep-03 22:52 
Generalhelp with algorithm Pin
coda_x6-Sep-03 17:24
coda_x6-Sep-03 17:24 
GeneralRe: Need help with idea Pin
Andrew Walker6-Sep-03 14:45
Andrew Walker6-Sep-03 14:45 
GeneralRe: Need help with idea Pin
Steven M Hunt6-Sep-03 15:57
Steven M Hunt6-Sep-03 15:57 
GeneralNeed help doing a simple game, ttt Pin
Snyp6-Sep-03 13:15
Snyp6-Sep-03 13:15 
I am making a project for school that is a game of tic-tac-toe. It is the console c++ not visual although I could do that but first I need one in console. See I need to make it work somehow and I was thinking of making an
if(user_inputbox1)<br />
 ...<br />
if(user_inputbox2)<br />
 ...

but for now I just have...
#include <iostream><br />
#include <cstdlib><br />
using namespace std;<br />
<br />
char matrix [3] [3];	//Tic Tac Toe Matrix<br />
<br />
char check();<br />
void init_matrix();<br />
void get_player_move();<br />
void get_computer_move();<br />
void disp_matrix();<br />
<br />
int main()<br />
{<br />
	char done = ' ';<br />
<br />
	init_matrix();<br />
<br />
	do{<br />
		disp_matrix();<br />
		get_player_move();<br />
		done = check();			//See if any winner<br />
		if(done != ' ') break;	//Winner!!!<br />
		get_computer_move();<br />
		done = check();			//See if any winner again<br />
	} while(done == ' ');<br />
<br />
	if(done=='X') <br />
		cout << "You won!\n";<br />
	else<br />
		cout << "I won!!!!\n";<br />
<br />
	disp_matrix();				//Display final matrix<br />
<br />
	return 0;<br />
}<br />
<br />
//Initialize the matrix<br />
void init_matrix()<br />
{<br />
	int i, j;<br />
<br />
	for(i=0; i<3; i++)<br />
		for(j=0; j<3; j++) matrix[i][j] =  ' ';<br />
}<br />
<br />
//Get the player's move<br />
void get_player_move()<br />
{<br />
	int x, y;<br />
<br />
	cout << "Enter X,Y coordinates for your move: ";<br />
	cin >> x;<br />
	cin >> y;<br />
<br />
	x--; y--;<br />
<br />
	if(matrix[x][y]!= ' '){<br />
		cout << "Invalid move, try again.\n";<br />
		get_player_move();<br />
	}<br />
  else matrix[x][y] = 'X';<br />
}<br />
<br />
//Get a move from the computer<br />
void get_computer_move()<br />
{<br />
	int i, j;<br />
<br />
f:	i = rand() % 3;<br />
	j = rand() % 3;<br />
	<br />
	if(matrix[i][j] !=' ')<br />
		goto f;<br />
<br />
	if(i*j==9)  {<br />
		cout << "draw\n";<br />
		system("pause");<br />
	}<br />
	else<br />
		matrix[i][j] = 'O';<br />
<br />
<br />
<br />
}<br />
<br />
//Display the matrix<br />
void disp_matrix()<br />
{<br />
	int t;<br />
<br />
	for(t=0; t<3; t++) {<br />
		cout << matrix[t][0] << "  | " << matrix[t][1] << " | " << matrix[t][2];<br />
		if(t != 2) <br />
			cout << "\n---|---|---\n";<br />
	}<br />
	cout << "\n";<br />
}<br />
<br />
//See if any winner<br />
char check()<br />
{<br />
	int i;<br />
<br />
	for(i=0; i<3; i++)  /* check rows */<br />
		if(matrix[i][0]==matrix[i][1] && matrix[i][0]==matrix[i][2]) <br />
			return matrix[i][0];<br />
<br />
	for(i=0; i<3; i++)  /* check columns */<br />
		if(matrix[0][i]==matrix[1][i] && matrix[0][i]==matrix[2][i]) <br />
			return matrix[0][i];<br />
<br />
	//Test diagonals for winner<br />
	if(matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2])<br />
		return matrix[0][0];<br />
<br />
	if(matrix[0][2]==matrix[1][1] && matrix[1][1]==matrix[2][0])<br />
		return matrix[0][2];<br />
<br />
  return ' ';<br />
}

Please make suggestions, comments, even brutal statements to make this a working, awesome app.. since the rand() is way off the computer move cause I just put it there to make it look how the comp would act.Confused | :confused:
MAINLY TRYING TO SOLVE THE GET_COMP METHOD TO MAKE THE COMP IF NOT UNDEFEATED THEN AT LEAST MAKE A DRAW...Blush | :O

<marquee>Universal Project... Soon to be a .net
GeneralRe: Need help doing a simple game, ttt Pin
Steven M Hunt6-Sep-03 13:30
Steven M Hunt6-Sep-03 13:30 
GeneralSocks Sock Socks! Pin
ez_way6-Sep-03 10:34
ez_way6-Sep-03 10:34 
GeneralRe: Socks Sock Socks! Pin
Jörgen Sigvardsson6-Sep-03 11:31
Jörgen Sigvardsson6-Sep-03 11:31 
Generalprocessing headers more efficiently Pin
Jim Crafton6-Sep-03 8:13
Jim Crafton6-Sep-03 8:13 
GeneralRe: processing headers more efficiently Pin
Andrew Walker6-Sep-03 15:07
Andrew Walker6-Sep-03 15:07 
GeneralRe: processing headers more efficiently Pin
Jim Crafton7-Sep-03 6:17
Jim Crafton7-Sep-03 6:17 
GeneralRe: processing headers more efficiently Pin
Serge Krynine8-Sep-03 0:18
Serge Krynine8-Sep-03 0:18 
GeneralUnderstanding HEAP: Free Heap block errors Pin
Jim Crafton6-Sep-03 7:34
Jim Crafton6-Sep-03 7:34 
GeneralRe: Understanding HEAP: Free Heap block errors Pin
Rama Krishna Vavilala6-Sep-03 11:05
Rama Krishna Vavilala6-Sep-03 11:05 
GeneralRe: Understanding HEAP: Free Heap block errors Pin
Jim Crafton6-Sep-03 11:08
Jim Crafton6-Sep-03 11:08 
QuestionFastest Text api? Pin
Roggan6-Sep-03 7:18
Roggan6-Sep-03 7:18 
GeneralSendMessage() Pin
lpRomang6-Sep-03 5:44
lpRomang6-Sep-03 5:44 
GeneralRe: SendMessage() Pin
Michael Dunn6-Sep-03 5:58
sitebuilderMichael Dunn6-Sep-03 5:58 
QuestionCView in DLL, CDocument in EXE :: How to share? Pin
clintsinger6-Sep-03 5:40
clintsinger6-Sep-03 5:40 
QuestionRegDeleteValue with REG_MULTI_SX/MoveFileEx? Pin
Kayembi6-Sep-03 5:16
Kayembi6-Sep-03 5:16 
AnswerRe: RegDeleteValue with REG_MULTI_SX/MoveFileEx? Pin
Kayembi7-Sep-03 2:35
Kayembi7-Sep-03 2:35 
GeneralRe: RegDeleteValue with REG_MULTI_SX/MoveFileEx? Pin
Kayembi7-Sep-03 3:49
Kayembi7-Sep-03 3:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.