c++ game

wilkila

New Member
Hi everyone.
Well im a noob at c++ and everything else and im trying to make a simple tic tac toe game.
I got a code done but when I compile it it say that i have errors that i cant fix
well this is the code

#include <allegro.h>

BITMAP *xSprite;
BITMAP *oSprite;

int board[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0}; //This will be used to keep
//track of the Xs and Os
int curSquare = 0; //This will keep track of the current square
//the selector is on

int turn = 1; //This will keep track of whose turn it is
//1 Will be for X and 2 for O

int x = 0; //X and Y position of selector
int y = 0;

int tempX = 0; //holds temporary values used to clear selector
int tempY = 0;

void setupBoard(){ //This function will draw in the grid

acquire_screen();

line( screen, 200, 0, 200, 480, makecol( 255, 255, 255));
line( screen, 400, 0, 400, 480, makecol( 255, 255, 255));
line( screen, 0, 150, 680, 150, makecol( 255, 255, 255));
line( screen, 0, 300, 680, 300, makecol( 255, 255, 255));

rect( screen, x+1, y+1, x + 199, y + 149, makecol( 255, 255, 0));

release_screen();

}

void updateBoard(){ //draws in selector

rect( screen, tempX+1, tempY+1, tempX + 199, tempY + 149, makecol( 0, 0, 0));
rect( screen, x+1, y+1, x + 199, y + 149, makecol( 255, 255, 0));
rest(100);
}

void announceWinner(){ //Announces the winner


if( turn == 1){
textout_ex( screen, font, "X Wins!!!!", 300, 240, makecol( 255, 0, 0), makecol(0, 0, 0));
} else {
textout_ex( screen, font, "O Wins!!!!", 300, 240, makecol( 255, 0, 0), makecol(0, 0, 0));
}


}

void checkWin(){ //checks for a winner

if( board[0] == turn && board[1] == turn && board[2] == turn){
announceWinner();
} else if( board[0] == turn && board[3] == turn && board[6] == turn){
announceWinner();
} else if( board[0] == turn && board[4] == turn && board[8] == turn){
announceWinner();
} else if( board[1] == turn && board[4] == turn && board[7] == turn){
announceWinner();
} else if( board[2] == turn && board[4] == turn && board[6] == turn){
announceWinner();
} else if( board[2] == turn && board[5] == turn && board[8] == turn){
announceWinner();
} else if( board[3] == turn && board[4] == turn && board[5] == turn){
announceWinner();
} else if( board[6] == turn && board[7] == turn && board[8] == turn){
announceWinner();
}

}



void drawXO(){ //draws in the X and O

acquire_screen();

if(turn == 1){
draw_sprite( screen, xSprite, x, y);
board[curSquare] = 1;
checkWin();
++turn;
} else if( turn == 2){
draw_sprite( screen, oSprite, x, y);
board[curSquare] = 2;
checkWin();
--turn;
}

release_screen();

rest(100);

}

void moveBox(){ //takes input

clear_keybuf();
tempX = x;
tempY = y;

if( key[KEY_UP] && y != 0){

y -= 150;
curSquare -=3;
updateBoard();

} else if( key[KEY_DOWN] && y != 300){

y += 150;
curSquare +=3;
updateBoard();

} else if( key[KEY_RIGHT] && x != 400){

x += 200;
++curSquare;
updateBoard();

} else if( key[KEY_LEFT] && x != 0){

x -= 200;
--curSquare;
updateBoard();

} else if( key[KEY_ENTER] && board[curSquare] == 0){

drawXO();

}

}

int main(){

allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);

xSprite = load_bitmap( "x.bmp", NULL);

oSprite = load_bitmap( "o.bmp", NULL);

setupBoard();

while( !key[KEY_ESC]){
moveBox();

}

destroy_bitmap( xSprite);
destroy_bitmap( oSprite);

return 0;

}
END_OF_MAIN();
please does anybody know whats wrong with it anything wuld help :confused:
 
I can't say I can help because I do java, but It might be handy for someone who COULD help you if you posted the error message too. :)
 
I can't say I can help because I do java, but It might be handy for someone who COULD help you if you posted the error message too. :)
Ohh yeah thank you
the error message is:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Dev-Cpp\calculator.cpp" -o "C:\Dev-Cpp\calculator.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\Dev-Cpp\calculator.cpp:1:21: allegro.h: No such file or directory
C:\Dev-Cpp\calculator.cpp:3: error: expected constructor, destructor, or type conversion before '*' token

C:\Dev-Cpp\calculator.cpp:3: error: expected `,' or `;' before '*' token
C:\Dev-Cpp\calculator.cpp:4: error: expected constructor, destructor, or type conversion before '*' token
C:\Dev-Cpp\calculator.cpp:4: error: expected `,' or `;' before '*' token
C:\Dev-Cpp\calculator.cpp: In function `void setupBoard()':
C:\Dev-Cpp\calculator.cpp:22: error: `acquire_screen' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:22: error: (Each undeclared identifier is reported only once for each function it appears in.)
C:\Dev-Cpp\calculator.cpp:24: error: `screen' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:24: error: `makecol' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:24: error: `line' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:29: error: `rect' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:31: error: `release_screen' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp: In function `void updateBoard()':
C:\Dev-Cpp\calculator.cpp:37: error: `screen' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:37: error: `makecol' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:37: error: `rect' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:39: error: `rest' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp: In function `void announceWinner()':
C:\Dev-Cpp\calculator.cpp:46: error: `screen' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:46: error: `font' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:46: error: `makecol' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:46: error: `textout_ex' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp: In function `void drawXO()':
C:\Dev-Cpp\calculator.cpp:80: error: `acquire_screen' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:83: error: `screen' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:83: error: `xSprite' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:83: error: `draw_sprite' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:88: error: `oSprite' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:94: error: `release_screen' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:96: error: `rest' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp: In function `void moveBox()':
C:\Dev-Cpp\calculator.cpp:102: error: `clear_keybuf' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:106: error: `key' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:106: error: `KEY_UP' undeclared (first use this function)

C:\Dev-Cpp\calculator.cpp:112: error: `KEY_DOWN' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:118: error: `KEY_RIGHT' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:124: error: `KEY_LEFT' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:130: error: `KEY_ENTER' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp: In function `int main()':
C:\Dev-Cpp\calculator.cpp:140: error: `allegro_init' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:141: error: `install_keyboard' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:142: error: `set_color_depth' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:143: error: `GFX_AUTODETECT' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:143: error: `set_gfx_mode' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:145: error: `xSprite' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:145: error: `NULL' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:145: error: `load_bitmap' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:147: error: `oSprite' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:151: error: `key' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:151: error: `KEY_ESC' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp:156: error: `destroy_bitmap' undeclared (first use this function)
C:\Dev-Cpp\calculator.cpp: At global scope:
C:\Dev-Cpp\calculator.cpp:162: error: expected constructor, destructor, or type conversion before ';' token

Execution terminated
 
Looks like youve got more then just one little error ;). Unfortunately my programming knowledge starts at html and stops at C#, c++ was too late to the party :(. But I can give you some advice. What IDE are you using? Try the visual C++ 2005 (2008) if you dont already have it. Its free!

It points out errors so simply and gives you tons of help to fix them :).
 
You should take care of the little problem first and see if the rest resolves. I'm almost positive you have at least one cascading error.

Start here: C:\Dev-Cpp\calculator.cpp:1:21: allegro.h: No such file or directory

After you find this file and put it in the proper directory, it's not a standard c++ library so it's probably not in the compiler provided includes directory by default. You should probably put it in with your c++ source file. Once you've found it and put it in the proper directory try compiling again, this one might resolve all the rest.

c++ was too late to the party
What do you mean too late to the party? C++ was well before HTML and C# ;)
 
Back
Top