Need help with checking string vallues
I'm writing a Tic-Tac-Toe game, but I'm having trouble with my checkwin() function at the bottom.
It's supposed to compare the values of each board[] but If i just press enter once, and it assigns X to board 1, it automatically says that I won.
Code:
//Tic-Tac-Toe
#include <windows.h>
#include <iostream>
#include <conio.h>
#include <string>
enum
{
W = 119,
A = 97,
S = 115,
D = 100,
ENTER = 13
};
int ch;
using namespace std;
int X;//for the coordinates
int Y;
int cell, player;
short int turn;
char pice, *pice_ptr = &pice;
int moveto( int moveX, int moveY );
static int get_code();
int XO();
int checkwin();
int checkcell();
void checkplayer();
void getcell();
int main(){
cout<<"\t\t\t Tic-Tac-Toe";
cout<<"\n\n\n\n\n\n\n\n";
cout<<"\t\t\t | |\n";
cout<<"\t\t\t | |\n";
cout<<"\t\t\t_____|_____|_____\n";
cout<<"\t\t\t | |\n";
cout<<"\t\t\t | |\n";
cout<<"\t\t\t_____|_____|_____\n";
cout<<"\t\t\t | |\n";
cout<<"\t\t\t | |\n";
cout<<"\t\t\t | |\n";
X = 26;
Y = 9;
moveto(X,Y); //start the game in cell 1
turn = 0;
while ( turn <= 12 || !checkwin() == 1 ){
ch = getch(); //get a keypress (W,A,S,D, or ENTER)
//------------up------------------------
switch (ch) {
case W:
if ( Y == 9 ){
break;
}
else{
Y = Y - 3;
moveto(X,Y);
break;
}
//-----------left----------------------
case A:
if ( X <= 26 ){
break;
}
else{
X = X - 6;
moveto(X,Y);
break;
}
//------------down----------------------
case S:
if ( Y == 15 ){
break;
}
else{
Y = Y + 3;
moveto(X,Y);
break;
}
//------------right--------------------------
case D:
if ( X >= 38 ){
break;
}
else{
X = X + 6;
moveto(X,Y);
break;
}
//------------enter---------------------
case ENTER:
XO();
checkwin();
break;
}
}
}
//mechanism for moving the cursor
int moveto( int moveX, int moveY )
{
HANDLE hOut;
COORD Position;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
Position.X = moveX;
Position.Y = moveY;
SetConsoleCursorPosition(hOut,Position);
}
static int get_code( void )
{
int ch = getch();
return ch;
}
void checkplayer_and_pice(){
if ( turn % 2 == 0 ){ //if there is a remainder, it is an odd number
player = 1;
pice = 88; //88 = letter O
}
else{
player = 2;
pice = 79;//79 = letter X
}
}
void getcell(){ //check's coordinate of cell and
if ( X == 26 && Y == 9 ){ cell = 1; } //give's cell a value according to
else if ( X == 26 && Y == 12 ){ cell = 4; } //the matching coordinates
else if ( X == 26 && Y == 15 ){ cell = 7; }
else if ( X == 32 && Y == 9 ){ cell = 2; }
else if ( X == 32 && Y == 12 ){ cell = 5; }
else if ( X == 32 && Y == 15 ){ cell = 8; }
else if ( X == 38 && Y == 9 ){ cell = 3; }
else if ( X == 38 && Y == 12){ cell = 6; }
else if ( X == 38 && Y == 15){ cell = 9; }
}
int checkcell(){ //checks the given board[] if it
if ( board[cell] == 0 ){ //already contains a value
return 0; // returns 1 if it does 0 otherwise
}
if ( !(board[cell] == 0) ){
return 1;
}
}
int XO() //mechanism for printing x or o to
{ //the screen
checkplayer_and_pice();
getcell();
checkcell();
if ( checkcell() == 0 ){
board[cell] = pice;
cout<<*pice_ptr;
turn++; //having a pointer is pointless -_-
return 0;
}
else if ( checkcell() == 1 ){
return 0;
}
}
int checkwin()
{
if ( board[1] == board[4] == board[7] ){
cout<<"\n\n\nPlayer "<<player<<" wins!";
return 1;
}
else{
return 0;
}
}