c++ Tron Game assignment

Hi there. I've been struggling with this game for weeks now. I'm to the point where the changes I'm making are making it worse. Two main issues here. One, the human player move only moves once in each direction. Needs to move until it "crashes" into the wall or the computer player. 2nd issue, computer player doesn't display a move at all on the board. Can anyone offer some suggestions?

Here's my code so far...


#include<iostream>
#include<string>
#include<conio.h>
#include<Windows.h>
#include<cstdlib>
#include<ctime>


using namespace std;
//declarations
bool gameOver=false;
const int ROW=23;
const int COL=80;
int humanRow;
int humanCol;
int computerRow;
int computerCol;
char board[ROW][COL];
char key;
string compMove;

void instruction();
void gameMap();
char humanMove (char input);
int computerMove();
void updateGameBoard();
bool crashTest();

int main()
{
instruction();//welcome and instructions function
system("cls");//clears screen to display board

board[10][20]='X';//starting position of human player
board[10][40]='*';//starting position of computer player
gameMap();//draws game map

while(!gameOver){

if (_kbhit())key=_getch();{
}
humanMove(key);
computerMove();
gameOver=crashTest();
updateGameBoard();


}

system("pause");
return 0;
}
void instruction(){
cout<<"\nWelcome to the game of Tron.\n\nTo move, use the following keys:\n\n";
cout<<"W'='Up\nX'='Down\nA'='Left\nD'='Right\n\n";
cout<<"Press any key to begin the game\n";


system("pause");
return;
}
void gameMap(){

for (int i=0;i<23;i++){
for (int j=0;j<80;j++){
board[0][j]='@';
board[22][j]='@';
board[i][0]='@';
board[i][79]='@';

if (board[i][j]=='X'){
board[i][j]='X';
}
else if (board[i][j]=='*'){
board[i][j]='*';
}

else board[i][j]=' ';
cout<<board[i][j];
}
}

return;
}

char humanMove(char key){

humanRow=10;
humanCol=20;

key=_getch();
key=toupper(key);
board[humanRow][humanCol];

switch(key) {
case 'W':humanRow++;humanCol;//move up
crashTest();

break;
case 'X':humanRow--;humanCol;//move down
crashTest();

break;
case 'A':humanRow;humanCol--;//move left
crashTest();
break;
case 'D':humanRow;humanCol++;//move right
crashTest();
break;
default: cout<<"Wrong Key,Please Try again";
break;
return (key);
}
}
int computerMove(){

int randMove;
randMove=(rand()%4+1);
if( (randMove=0)&&(board[computerRow][computerCol]!='*')){
computerRow++;computerCol;
return computerRow,computerCol;
crashTest();
}
else if ( (randMove=0)&&(board[computerRow][computerCol]=='*')){
computerRow--;computerCol;
return computerRow, computerCol;
crashTest();
}
else if((randMove=1)&&(board[computerRow][computerCol]!='*')){
computerRow--;computerCol;
return computerRow, computerCol;
crashTest();
}
else if ( (randMove=1)&&(board[computerRow][computerCol]=='*')){
computerRow++;computerCol;
return computerRow,computerCol;
crashTest();
}
else if((randMove=2)&&(board[computerRow][computerCol]!='*')){
computerRow;computerCol--;
return computerRow;computerCol;
crashTest();
}
else if ( (randMove=2)&&(board[computerRow][computerCol]=='*')){
computerRow;computerCol++;
return computerRow;computerCol;
crashTest();
}
else if((randMove=3)&&(board[computerRow][computerCol]!='*')){
computerRow;computerCol++;
return computerRow,computerCol;
crashTest();
}
else if ( (randMove=0)&&(board[computerRow][computerCol]=='*')){
computerRow;computerCol--;
return computerRow,computerCol;
crashTest();
}


else
cout<<"game over, computer crashes";

}


bool crashTest(){

if ((board[humanRow][humanCol]!=' ')||(board[computerRow][computerCol]!=' ')){
cout<<"\nGame Over";
gameOver=true;
return true;
}
else{
return false;
}



}
void updateGameBoard(){
board[humanRow][humanCol]=='X';
board[computerRow][computerCol]=='*';
//system("cls");
cout<<board[humanRow][humanCol]<<board[computerRow][computerCol];
return;
}
Ok, several things to look at:

main
1) gameMap(); should be called within the while loop to redraw every iteration
2) Do something like
1
2
3
4
5
    
    humanRow=10;
    humanCol=20;
    computerRow=10;
    computerCol=40;

to initialize these variables after you set board[10][40]='*';
3) Empty block after _getch(); is meaningless

humanMove
1) why does it return a value - you don't use it anyway
2) calling crashTest() in every clause makes no sense - you do it anyway in the main loop - you should remove it

computerMove
1) if (randMove=0). You should use the logical operator: if (randMove == 0)
2) This code has no sense at all - remove all instances
1
2
return computerRow,computerCol;
crashTest();

3) Also - why do you return something if you don't use it?
4) Last if states if ( (randMove==0) - I assume you meant if ( (randMove==3)

updateGameBoard()
Incorrect usage of logical operator board[humanRow][humanCol]=='X'; - to assign a value - you should write board[humanRow][humanCol]='X';

These are just the most prominent problems. There are so many more things you could improve - remove globals, encapsulate player information in a class, etc, etc, but I hope that you will be improving your code along with learning C++.
One more thing - computer tries to avoid player's trail, but not its own trail, so it will crash very soon.
Thank you for your tips. I know there is a lot of room for improvement. I do plan on working on making it better, just need to get it at least in a working state. Will correct the errors you mentioned and add in AI for computer trail.
Again, thank you!
Topic archived. No new replies allowed.