Aug 27, 2012 at 5:23pm UTC
Thanks. I didn't realise I forgot the curly brace. However, after that nothing happens . Whatever was on the screen stays, players cant even input move.
Aug 27, 2012 at 5:29pm UTC
print_Grid
function should return void
.
Aug 27, 2012 at 5:31pm UTC
For what i can see, your program has to be completely remade.
Try checking my Tic-Tac-Toe code (Windows only :c):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
char Choice;
char Player1[20];
char Player2[20];
int State[10]={0}; //State of the squares from 1 to 9
int Win;
void Color(char color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
class Game{public : //1All the functions are here
static void Square(int square) { //Used in the display
if (State[square]==0){ //If state is 0, shows the square number
Color(8); //Grey
cout << " " << square << " " ;
}
else if (State[square]==1){
Color(12); //Brigth Red
cout << " X " ; //If state is 1, X
}
else if (State[square]==2){
Color(9); //Brigth Blue
cout << " O " ; //If state is 2, O
}
Color(15); //Brigth White
};
static void Check(int c) { //Used in the main game code
if (c==0){ //If everything is used...
for (int x=1; State[x]>0; x++) {
if (x==9){Win=0; break ;} //It's a draw
}
}
else { //All the possible options of winning
if ((State[1]==c && State[2]==c && State[3]==c)
|| (State[4]==c && State[5]==c && State[6]==c)
|| (State[7]==c && State[8]==c && State[9]==c)
|| (State[1]==c && State[4]==c && State[7]==c)
|| (State[2]==c && State[5]==c && State[8]==c)
|| (State[3]==c && State[6]==c && State[9]==c)
|| (State[1]==c && State[5]==c && State[9]==c)
|| (State[3]==c && State[5]==c && State[7]==c)
){Win=c; return ;} //If someone wins, it shows it in the display
}
};
static void Display(int turn) { //Finally, the display itself.
system("cls" );
if (Win>2){ //If Win is'nt 0 or 1 or 2
if (turn==1){
Color(12);
cout << "\n --" << Player1 << "'s turn--" ;
}
if (turn==2){
Color(9);
cout << "\n --" << Player2 << "'s turn--" ;
}
Color(15);
cout << "\n Choose the square with numbers 1-9 to place an " ;
if (turn==1){cout << "\" X \".\n" ;} else {cout << "\" O \".\n" ;}
}
//All the square displaying
cout << "\n " ;
Game::Square(1); cout << "|" ;
Game::Square(2); cout << "|" ;
Game::Square(3); cout << "" ;
cout << "\n -----------\n " ;
Game::Square(4); cout << "|" ;
Game::Square(5); cout << "|" ;
Game::Square(6); cout << "" ;
cout << "\n -----------\n " ;
Game::Square(7); cout << "|" ;
Game::Square(8); cout << "|" ;
Game::Square(9); cout << endl;
if (Win==0){cout << "\n --Draw--" ; return ;}
if (Win==1){
Color(12);
cout << "\n --" << Player1 << " wins!--" ; return ;
}
if (Win==2){
Color(9);
cout << "\n --" << Player2 << " wins!--" ; return ;
}
Color(15);
};
static void Main(int turn) { // And finally, the main game code.
Display(turn); //Displays and chooses wich turn it is
Loop: Choice=getch();
switch (Choice) { //All the keys to display an X or O in the screen
default : goto Loop;
/*1*/ case '1' :
if (State[1]==0){State[1]=turn;} //Only editable if it has'nt been edited before
else {goto Loop;} //If it has been edited before, it loops the key scanning
break ;
/*2*/ case '2' :
if (State[2]==0){State[2]=turn;}
else {goto Loop;}
break ;
/*3*/ case '3' :
if (State[3]==0){State[3]=turn;}
else {goto Loop;}
break ;
/*4*/ case '4' :
if (State[4]==0){State[4]=turn;}
else {goto Loop;}
break ;
/*5*/ case '5' :
if (State[5]==0){State[5]=turn;}
else {goto Loop;}
break ;
/*6*/ case '6' :
if (State[6]==0){State[6]=turn;}
else {goto Loop;}
break ;
/*7*/ case '7' :
if (State[7]==0){State[7]=turn;}
else {goto Loop;}
break ;
/*8*/ case '8' :
if (State[8]==0){State[8]=turn;}
else {goto Loop;}
break ;
/*9*/ case '9' :
if (State[9]==0){State[9]=turn;}
else {goto Loop;}
break ;
/*case 'd': case 'D': Win=0; break;
case 'r': case 'R': Win=1; break;
case 'g': case 'G': Win=2; break;*/
}
Game::Check(1); //Checks if player 1 or player 2 wins
Game::Check(2);
if (Win==3){Game::Check(0);} //Then checks if it's a draw
if (Win<3){return ;}
};
}Game;
int main()
{
Start:
system("cls" ); Win=3;
int x=0; while (x<10){State[x]=0; x=x+1;}
Color(15);
cout << "\n .:Tic-Tac-Toe:.\n\n (Names can have 15 characters max. and be 1 word only)\n" ;
Color(12);
cout << "\n Player 1" ;
Color(15);
cout << ", choose a name\n@ " ;
cin >> Player1;
Color(9);
cout << "\n Player 2" ;
Color(15);
cout << ", choose a name\n@ " ;
cin >> Player2;
Game:
system("cls" ); Win=3;
x=0; while (x<10){State[x]=0; x=x+1;}
while (true )
{ //The turns
if (Win<3){Game.Display(0); goto Exit;}
Game.Main(1);
if (Win<3){Game.Display(0); goto Exit;}
Game.Main(2);
if (Win<3){Game.Display(0); goto Exit;}
}
Exit:
Color(15);
cout << "\n\n 1 = Play again\n 2 = Change names\n 3 = Exit\n\n@ " ;
cin >> Choice;
switch (Choice) {
default : break ;
case '1' : goto Game;
case '2' : goto Start;
}
cout << "\n Closing Window...\n" ;
return 0;
}
Please notice, this is a very old game I made, some extremely stupid things and use of goto could appear.
Last edited on Aug 27, 2012 at 5:38pm UTC
Aug 27, 2012 at 5:37pm UTC
@Samrux Your code contains too many goto
loops.
Aug 27, 2012 at 5:39pm UTC
Sorry, I did'nt notice your post, I edited mine 1 minute after:
Please notice, this is a very old game I made, some extremely stupid things and use of goto could appear.
I feel stupid looking at all those gotos...
Last edited on Aug 27, 2012 at 5:40pm UTC
Aug 27, 2012 at 5:41pm UTC
@samrux:
your last cout is useless unless they can see it :D (not that it changes the functionality). And if you only have one argument following a case in a switch, then you don't have to explicitly break? Sorry if these answers are obvious, but I'm really new to coding with anything that has switches.
Aug 27, 2012 at 5:52pm UTC
The last cout can be seen as long as the user doesn't pick 1 or 2. As for your second question, I suppose you mean something like this:-
1 2 3 4 5 6 7 8 9
switch (x)
{
case 1:
print 1;
break ; // this break statement is needed
case 2:
print 2;
break ; // this however isn't
}
Edit:
Change this:-
1 2 3 4 5 6
while (gameover == false )
{
void determine_turn();
void get_move();
void check_win();
}
To:-
1 2 3 4 5 6
while (gameover == false )
{
determine_turn();
get_move();
check_win();
}
You do not specify return type when calling functions.
Last edited on Aug 27, 2012 at 6:02pm UTC
Aug 27, 2012 at 6:02pm UTC
@ OP: Having a closer look at that print_grid function reveals many more errors, I'm surprised this would even compile:
- you have not initialised the G or Grid variables for as far as I can see, yet you're passing char G to the function.
- however, you're not using G, you're using Grid within that function.
So your funciton should be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void Print_Grid (char [] Grid) //void here, as Disch said
{
cout << "__________________________\n" ;
cout << " | | \n" ;
cout << Grid [0] && "|" && Grid [1] && "|" && Grid [2];
cout << " | | \n" ;
cout << "__________________________\n" ;
cout << " | | \n" ;
cout << Grid [3] && "|" && Grid [4]&& "|" && Grid [5];
cout << " | | \n" ;
cout << "__________________________\n" ;
cout << " | | \n" ;
cout << Grid [6] && "|" && Grid [7] && "|" && Grid [8];
cout << " | | \n" ;
cout << "__________________________\n" ;
} // closing curly brace here
You will also have to initialise the Grid array, or it will not contain any values for you to display.
Easiest way to do so is:
1 2
//line 15
char Grid[9] = { '_' , '_' , '_' , '_' , '_' , '_' , '_' , '_' , '_' }; //fill array with underscores
Last edited on Aug 27, 2012 at 6:02pm UTC
Aug 27, 2012 at 6:08pm UTC
Use while loops in place of all your goto
instead. Goto's are so bad in programming.
Aug 27, 2012 at 6:14pm UTC
The OP's code doesn't contain goto's - 2 different tic tac toe programs in one thread is confusing.
-N
Aug 28, 2012 at 2:07am UTC
Ok. i got rid of the voids in the main program to call the subprograms, but it did not pick that up. Instead it said 'determine_move', and the other two were not declared in this scope.