#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
Tic Tac Toe question???
Well here is my ttt game.
Suggestions:
char turn = ''X'';char board[9];...// instead of the big switch statement...if (board[move] != '' '') board[move] = turn;else // error, place already taken!
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
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
Dim turnX As BooleanPrivate 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 IfEnd SubPrivate Sub Form_Load() turnX = TrueEnd 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)
If it easier to write programs with VB then why doesn''t everybody use it. What are some commercial programs that use VB
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.
VB is a tad more productive than C++, but C++ is alot faster and flexible.
---------------------------------------------------
laziness is the foundation of efficiency
retrospiral.net | llamas! | megatokyo | FreeBSD | gamedev.net | google
---------------------------------------------------
laziness is the foundation of efficiency
retrospiral.net | llamas! | megatokyo | FreeBSD | gamedev.net | google
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement