I'm currently devoloping code for a version of the game Connect 4 that was assigned to me and right now I'm at the initial stages. My problem is getting the move from the player and putting it on the board. I thought I had it figured it, when I compile I get no errors, however when I go to execute I get an error from microsoft saying that the .exe file has encountered an error and needs to close. The games played with X's and O's so I made an array called board. If anyone can take a look at my code and let me know what I'm doing wrong I would appreciate it.
Here's what the first part of the assignment asks:
Declare a 1D array in the function main which records the
moves which have been made. (Hint: you know which player
made each move -- the even moves are made by X, the odd
moves by O.)
Declare a 2D array in the function main, which holds the
current state of the game.
Write functions clearScreen and showBoard which can be used
to redraw the board after each move. (Hint: showBoard must
be passed the board array as a parameter.)
Write a function getNextMove which:
prompts the user whose turn it is to enter a move (Hint:
getNextMove can figure this out if it knows which move
this is)
checks that it is a legal move (the column must be from
1 to 7, and the column can't already be full)
repeats the above until the player enters a legal move
returns the move
Write a loop in main which calls getNextMove, stores the
move in the board array and displays the current state of
the game, until the board is full.
Code:#include<iostream> #include<cstring> #include<iomanip> #include<fstream> void clearScreen (); void showBoard (char board[6][7]); void initializeBoard (char board[6][7]); void getNextMove(char board[6][7], char trackMove[], int MAXMOVES, int row, int column, int currentMove); using namespace std; int main () { const int MAXMOVES = 42; char board[6][7]; char trackMove[MAXMOVES]; int row = 5; int column; int currentMove; showBoard(board); for (currentMove = 0; currentMove < MAXMOVES; currentMove++) { getNextMove(board, trackMove, MAXMOVES, row, column, currentMove); board[row][column - 1] = trackMove[currentMove]; clearScreen (); showBoard(board); } return 0; } //Function Definitions void clearScreen ( void ) { for ( int i = 0; i < 50; i++ ) cout<<'\n'; cout<<flush; } void showBoard (char board[6][7]) { initializeBoard(board); cout<<setw(40)<<" Connect 4"<<endl; cout<<setw(30)<<endl; cout<<setw(30)<<"| "<<board[0][0]<<" "<<board[0][1]<<" "<<board[0][2]<<" "<<board[0][3]<<" "<<board[0][4]<<" "<<board[0][5]<<" "<<board[0][6]<<" |"<<endl; cout<<setw(30)<<"| "<<board[1][0]<<" "<<board[1][1]<<" "<<board[1][2]<<" "<<board[1][3]<<" "<<board[1][4]<<" "<<board[1][5]<<" "<<board[1][6]<<" |"<<endl; cout<<setw(30)<<"| "<<board[2][0]<<" "<<board[2][1]<<" "<<board[2][2]<<" "<<board[2][3]<<" "<<board[2][4]<<" "<<board[2][5]<<" "<<board[2][6]<<" |"<<endl; cout<<setw(30)<<"| "<<board[3][0]<<" "<<board[3][1]<<" "<<board[3][2]<<" "<<board[3][3]<<" "<<board[3][4]<<" "<<board[3][5]<<" "<<board[3][6]<<" |"<<endl; cout<<setw(30)<<"| "<<board[4][0]<<" "<<board[4][1]<<" "<<board[4][2]<<" "<<board[4][3]<<" "<<board[4][4]<<" "<<board[4][5]<<" "<<board[4][6]<<" |"<<endl; cout<<setw(30)<<"| "<<board[5][0]<<" "<<board[5][1]<<" "<<board[5][2]<<" "<<board[5][3]<<" "<<board[5][4]<<" "<<board[5][5]<<" "<<board[5][6]<<" |"<<endl; cout<<setw(44)<<" _ _ _ _ _ _ _ "<<endl; cout<<setw(44)<<" 1 2 3 4 5 6 7 "<<endl; cout<<endl; cout<<endl; cout<<endl; cout<<endl; } void initializeBoard (char board[6][7]) { for(int row = 0; row < 6; row++) for(int col = 0; col < 7; col++) board[row][col] = ' '; } void getNextMove(char board[6][7], char trackMove[], int MAXMOVES, int row, int column, int currentMove) { if (currentMove % 2 == 0) { cout<<"Player 1 make your move"; cout<<endl; cout<<endl; cout<<"Enter a column (1-7): "; cin>>column; cout<<endl; cout<<endl; while (!(column <=7 && column >=1)) { cout<<"Please enter a legal move! A number between 1-7 "; cin>>column; cout<<endl; } trackMove[currentMove] = 'X'; row--; } else { cout<<"Player 2 make your move."; cout<<endl; cout<<endl; cout<<"Enter a column (1-7): "; cin>>column; cout<<endl; cout<<endl; while (!(column <=7 && column >=1)) { cout<<"Please enter a legal move! A number between 1-7 "; cin>>column; cout<<endl; } trackMove[currentMove] = 'O'; row--; } }



LinkBack URL
About LinkBacks


