Advertisement

Can some one please help

Started by October 08, 2002 08:26 PM
0 comments, last by SSJCORY 22 years, 1 month ago
I have made a very simple tic tac toe game w/ comments but I cannot figure out how to make it so if player 1 picks a spot player 2 cannot pick the same spot. Any help would be greatly appreciated. #include//Header to allow input and output using cin and cout char a[1];// First section of tic tac toe board char b[1];// Second section of tic tac toe board char c[1];// Third section of tic tac toe board char d[1];// Fourth section of tic tac toe board char e[1];// Fifth section of tic tac toe board char f[1];// Sixth section of tic tac toe board char g[1];// Seventh section of tic tac toe board char h[1];// Eighth section of tic tac toe board char i[1];// Nineth section of tic tac toe board int ponemove; int ptwomove; ponewin();//Player ones winning message declaration ponewin(){//Player ones winning message code cout<<"Player one wins the game!!!\n"; } ptwowin();//Player twos winning message declaration ptwowin(){//Player twos winning message code cout<<"Player two wins the game!!!\n"; } getposition();//Get moves of both players declaration getposition(){//Get moves of both players code char a=''1'';//Setting board 1,1 to display 1 char b=''2'';//Setting board 1,2 to display 2 char c=''3'';//Setting board 1,3 to display 3 char d=''4'';//Setting board 2,1 to display 4 char e=''5'';//Setting board 2,2 to display 5 char f=''6'';//Setting board 2,3 to display 6 char g=''7'';//Setting board 3,1 to display 7 char h=''8'';//Setting board 3,2 to display 8 char i=''9'';//Setting board 3,3 to display 9 do{///////////////////////////Do While loop//////////////////// cout<<"\t\t************\n"; //Display cout<<"\t\t* "<>ponemove; if(ponemove==1){//if player 1 move = 1 assign x to a a=''x''; } if(ponemove==2){//if player 1 move = 2 assign x to b b=''x''; } if(ponemove==3){//if player 1 move = 3 assign x to c c=''x''; } if(ponemove==4){//if player 1 move = 4 assign x to d d=''x''; } if(ponemove==5){//if player 1 move = 5 assign x to e e=''x''; } if(ponemove==6){//if player 1 move = 6 assign x to f f=''x''; } if(ponemove==7){//if player 1 move = 7 assign x to g g=''x''; } if(ponemove==8){//if player 1 move = 8 assign x to h h=''x''; } if(ponemove==9){//if player 1 move = 9 assign x to i i=''x''; } if(a==''x''&&b==''x''&&c==''x''){// if combination of 3 is true{{{ ponewin(); return(0); } if(d==''x''&&e==''x''&&f==''x''){ ponewin(); return(0); } if(g==''x''&&h==''x''&&i==''x''){ ponewin(); return(0); } if(a==''x''&&d==''x''&&g==''x''){ ponewin(); return(0); } if(b==''x''&&e==''x''&&h==''x''){ ponewin(); return(0); } if(c==''x''&&f==''x''&&i==''x''){ ponewin(); return(0); } if(a==''x''&&e==''x''&&i==''x''){ ponewin(); return(0); } if(c==''x''&&e==''x''&&g==''x''){ ponewin(); return(0); } //}}} cout<<"\n\n\n"; cout<<"\t\t************\n"; //Same as above{ cout<<"\t\t* "<>ptwomove; cout<<"\n\n\n"; if(ptwomove==1){//Same as above except assigns o to variable{ a=''o''; } if(ptwomove==2){ b=''o''; } if(ptwomove==3){ c=''o''; } if(ptwomove==4){ d=''o''; } if(ptwomove==5){ e=''o''; } if(ptwomove==6){ f=''o''; } if(ptwomove==7){ g=''o''; } if(ptwomove==8){ h=''o''; } if(ptwomove==9){ i=''o''; } //}}} if(a==''o''&&b==''o''&&c==''o''){//IF any 3 combinations = x player 2 wins ptwowin(); return(0); } if(d==''o''&&e==''o''&&f==''o''){ ptwowin(); return(0); } if(g==''o''&&h==''o''&&i==''o''){ ptwowin(); return(0); } if(a==''o''&&d==''o''&&g==''o''){ ptwowin(); return(0); } if(b==''o''&&e==''o''&&h==''o''){ ptwowin(); return(0); } if(c==''o''&&f==''o''&&i==''o''){ ptwowin(); return(0); } if(a==''o''&&e==''o''&&i==''o''){ ptwowin(); return(0); } if(c==''o''&&e==''o''&&g==''o''){ ptwowin(); return(0); } ///}}}} } while(ponemove!=100);//END OF DO WHILE LOOP } main(){//Main Function getposition();//calls get position as defined above cout<<"TICK TACK TOE V 1.0\n";//Displays v # cout<<"BY:CORY FISHER\n";//Displays my name return(0);//Returns zero } Cory Fisher
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
First of all, I don''t understand why you''re declaring 9 one element arrays. I''m guessing that you are very new to programming as it seems you don''t really understand the concept of arrays. What you should do is define one array with 9 elements to store the board:

Eg. char board[9]; // Array with elements from 0 to 8 (9 elems)

You can initialise this using a simple ''for'' loop.

Eg. for(int i=0; i<9; i++)
board='' ''; // Sets element ''i'' to blank space

Then to check/prevent double usage of a space is as simple as checking if a space is not a blank space.
Eg. if(board[x]!='' '')
// Double Usage
else
// Change board[x] to ''O'' or ''X''

Honestly though, I really think you should read more on programming concepts. Once you understand arrays, this program will be reduced to about 1/9th the size.

Good luck.

This topic is closed to new replies.

Advertisement