Jump to content
  • Advertisement

Archived

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

librab103

Tic Tac Toe question???

This topic is 5466 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
what are you trying to say ... my code is inadequate. I hope you was kidding about saying my code can be fitted on 5 lines in vb

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
If it easier to write programs with VB then why doesn''t everybody use it. What are some commercial programs that use VB

Share this post


Link to post
Share on other sites
quote:
Original post by librab103
If it easier to write programs with VB then why doesn''t everybody use it. What are some commercial programs that use VB


VB is easier for simple things, but most prefer C++ for larger projects.

Share this post


Link to post
Share on other sites
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???

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!