Archived

This topic is now archived and is closed to further replies.

librab103

Tic Tac Toe question???

Recommended Posts

Well here is my ttt game.
#include <stdio.h>
#include <windows.h>

int turn, move;
char name1[25], name2[25];
char board[3][3]; //The tic-tac-toe Board



void DrawBoard();
void DrawBoard() //* The board *//

{
printf("\n\n");
printf(" %c | %c | %c \n", board[0][0], board[0][1], board[0][2]);
printf(" --------- \n");
printf(" %c | %c | %c \n", board[1][0], board[1][1], board[1][2]);
printf(" --------- \n");
printf(" %c | %c | %c \n", board[2][0], board[2][1], board[2][2]);
printf("\n\n");
}


int main()

{
BOOL StillPlaying=TRUE;
int move=0;
int turn=1;

printf("Welcome to tic-tac-toe!!!\n\n");

//players names

printf("What is the first player's name: ");	
	scanf("%s", name1);
printf("\nWhat is the second player's name: ");
	scanf("%s", name2);
	
while (StillPlaying)
	{
		DrawBoard();

	if (turn == 1){
		printf("It is %s turn!\n", name1);
		scanf("%d", &move);
		turn=2;
		printf("\n");
		}
	else{
		printf("It is %s turn!\n", name2);
		scanf("%d", &move);
		turn=1;
		printf("\n");
	} 
switch(move)
	{
		
		case 1: if (turn==1){board[0][0]='X'; break;}
				else {board[0][0]='O'; continue;}
		case 2: if (turn==1){board[0][1]='X'; break;}
				else {board[0][1]='O'; continue;}
		case 3: if (turn==1){board[0][2]='X'; break;}
				else {board[0][2]='O'; continue;}
		case 4: if (turn==1){board[1][0]='X'; break;}
				else {board[1][0]='O'; continue;}
		case 5: if (turn==1){board[1][1]='X'; break;}
				else {board[1][1]='O'; continue;}
		case 6: if (turn==1){board[1][2]='X'; break;}
				else {board[1][2]='O'; continue;}
		case 7: if (turn==1){board[2][0]='X'; break;}
				else {board[2][0]='O'; continue;}
		case 8: if (turn==1){board[2][1]='X'; break;}
				else {board[2][1]='O'; continue;}
		case 9: if (turn==1){board[2][2]='X'; break;}
				else {board[2][2]='O'; continue;}
		case 10: StillPlaying = FALSE;
				 printf("Leaving so soon?\n");		
				 break;
	} // switch(move)


					

}//While Winner


return 0;	

}// int main()  

Its like in the alpha stage, you can input the player names and take turns. If you looked at the code you can tell 2 things. 1-is that I don't have code for checking for double usage of a spot and 2-to check to see if someone has won. Right now I trying to figure out the double usage thing. Any ideas??? Thanks in advance

Share this post


Link to post
Share on other sites
Suggestions:

char turn = ''X'';
char board[9];

...

// instead of the big switch statement...
if (board[move] != '' '')
board[move] = turn;
else
// error, place already taken!

Share this post


Link to post
Share on other sites
this is the kind of things I think everyone would agree VB is better for. Everything you made so far(+double moving checking ) would be aproximately 5 lines of code

Share this post


Link to post
Share on other sites
Dim turnX As Boolean
Private Sub Command1_Click(Index As Integer)
If Command1(Index).Caption = "" Then
If turnX Then Command1(Index).Caption = "
x"
If turnX = False Then Command1(Index).Caption = "
o"
turnX = Not turnX
End If
End Sub
Private Sub Form_Load()
turnX = True
End Sub


That is all the code - except the name inputing thing - took me less than 3 minutes including the gui that had to be made.

Your code isn''t bad just there are languages that require less code ( you are right it''s more than 5 lines)

Share this post


Link to post
Share on other sites
quote:
Original post by librab103
To be able to use and be very knowledgeable in C or C++, before you can make larger projects and what not, is to make simple programs like ttt... am i right???


Very true.... The basics are the most important of pretty much any programing language, if not anything. The basics are very easily learnt by making multiple small projects.

Suggestions:
Do a newer version in C++.
Once you do that, make another one (or a text-RPG/adventure) using classes and OOP.



Rob Loach
Current Project: Go Through Object-Oriented Programming in C++ by Robert Lafore

"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?"
- The Boondock Saints

Share this post


Link to post
Share on other sites
I guess that is what I am going to do. After I finish making a 2 player ttt game, I will make a 1 player and make a decent AI. Then I was thinking about doing a connect 4 or Tetris game in C++ just so I can get use to cout and whatever C++ uses. After that make a rpg text game, maybe with some of those graphics from back in the day (the early 80's) if you know what I mean. I hope this doesn't take forever.



(friend) WHAT!!!!..... Are you crazy???
(me) Yes I am.

[edited by - librab103 on July 3, 2003 3:46:16 AM]

Share this post


Link to post
Share on other sites
I did what twix said about removing the switch statements:


#include <stdio.h>
#include <windows.h>

int turn, move;
char name1[25], name2[25];
char board [9]; // The tic-tac-toe Board



void DrawBoard();
void DrawBoard() //* Draws the board *//

{
printf("\n\n");
printf(" %c | %c | %c \n", board[1], board[2], board[3]);
printf(" --------- \n");
printf(" %c | %c | %c \n", board[4], board[5], board[6]);
printf(" --------- \n");
printf(" %c | %c | %c \n", board[7], board[8], board[9]);
printf("\n\n");
}

void CheckSpace();
void CheckSpace() // Checks for double usage and places X's and O's

{
}
int main()

{
BOOL StillPlaying=TRUE;
int move=0;
int turn=1;

printf("Welcome to tic-tac-toe!!!\n\n");

//players names

printf("What is player 1's name: ");
scanf("%s", name1);
printf("\nWhat is player 2's name: ");
scanf("%s", name2);

while (StillPlaying)
{
DrawBoard();

if (turn == 1){
printf("Please make a move %s : ", name1);
scanf("%d", &move);
turn=2;
printf("\n");
}
else{
printf("Please make a move %s : ", name2);
scanf("%d", &move);
turn=1;
printf("\n");
}

{
if (turn == 1)
{board[move] = 'X';}
else
{board[move] = 'O';}
}

}//While Winner


return 0;

}// int main()



I Removed like 14 lines of code or so but now I have a new problem. You see the code where assigns an X or O, well if I do this:

   
#include <stdio.h>
#include <windows.h>

int turn, move;
char name1[25], name2[25];
char board [9]; // The tic-tac-toe Board



void DrawBoard();
void DrawBoard() //* Draws the board *//

{
printf("\n\n");
printf(" %c | %c | %c \n", board[1], board[2], board[3]);
printf(" --------- \n");
printf(" %c | %c | %c \n", board[4], board[5], board[6]);
printf(" --------- \n");
printf(" %c | %c | %c \n", board[7], board[8], board[9]);
printf("\n\n");
}

void CheckSpace();
void CheckSpace() // Checks for double usage and places X's and O's

{
if (turn == 1)
{board[move] = 'X';}
else
{board[move] = 'O';}
}


int main()

{
BOOL StillPlaying=TRUE;
int move=0;
int turn=1;

printf("Welcome to tic-tac-toe!!!\n\n");

//players names

printf("What is player 1's name: ");
scanf("%s", name1);
printf("\nWhat is player 2's name: ");
scanf("%s", name2);

while (StillPlaying)
{
DrawBoard();

if (turn == 1){
printf("Please make a move %s : ", name1);
scanf("%d", &move);
turn=2;
printf("\n");
}
else{
printf("Please make a move %s : ", name2);
scanf("%d", &move);
turn=1;
printf("\n");
}


CheckSpace();

}//While Winner


return 0;

}// int main()


It doesn't seem to see the few lines of code!!! odd
Sorry for all the code posting in advance :-D



(friend) WHAT!!!!..... Are you crazy???
(me) Yes I am.


[edited by - librab103 on July 3, 2003 2:47:33 PM]

Share this post


Link to post
Share on other sites