Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int whichRow(char placement[9][7], int column);
- int checkWinner(char board[9][7], char key);
- int main()
- {
- cout << "Welcome to Connect 4!\nPlayer 1 has 'O' tokens and Player 2 has 'X' tokens\nLet's get started!\n\n";
- //variable decleration
- char gameBoard[9][7];
- int colNum, player, count, row, winner = 0;
- char token[2] = {'O', 'X'};
- bool gameOver = false;
- //fills the gameboard
- for(int i = 0; i < 9; i++){//rows
- for(int j = 0; j < 7; j++){//columns
- gameBoard[i][j] = ' ';
- }//end for
- }//end for (fill array)
- count = 0;
- //loops for constant user input until checkWinner returns true
- while(gameOver == false){
- player = count%2;
- cout << "Player " << player +1 << ", Choose a column (1-7):";
- cin >> colNum;
- //prompts user to enter a number within the bounds of the board
- while(colNum > 7 || colNum < 1){
- cout << "\nThat column does not exist.\nEnter a different number:";
- cin >> colNum;
- // row = whichRow(gameBoard, colNum-1);
- }//end while
- row = whichRow(gameBoard, colNum-1);
- //prompts user to enter a different column # if selected column is full
- while(row == -1){
- cout << "That column is full. Please select another: ";
- cin >> colNum;
- row = whichRow(gameBoard, colNum-1);
- }//end while
- gameBoard[row][colNum-1] = token[player];
- //prints gameboard with tokens
- for(int i = 0; i < 9; i++){//rows
- cout << "|";
- for(int j = 0; j < 7; j++){//columns
- cout << gameBoard[i][j] << "|";
- }//end for
- cout << endl;
- }//end for
- cout << "=============== \n";
- winner = checkWinner(gameBoard, token[player]);
- //prints the winner
- if(winner == 1){
- cout << endl << "Player " << player+1 << " is the winner!!!\n";
- cout << "Congratulations, you win 50 coins!\n\n";
- for(int i = 0; i < 50; i++)
- cout << " coin ";
- cout << "\n\nWhat can you do with these virtual coins? Nothing!\nHave a great day!\n";
- break;
- }//end if Game Over
- //used to alternate player turns
- count++;
- }//end while
- return 0;
- }//end main
- //checks if there is a token in the selected column and returns the index of the row to place the new token
- //checks for full rows and returns indicator to prompt user to enter a different row
- int whichRow(char placement[9][7], int column){
- int i;
- for(i = 8; i >= 0; i--){
- if(placement[i][column] == ' ')
- return i;
- if(placement[0][column] != ' ')
- return -1;
- }//end for
- return i-1;
- }//end whichRow
- //checks for a winner after every turn and returns 'true' if there is a winner and 'false' if there is not
- int checkWinner(char board[9][7], char key){
- //horizontal search
- for(int i = 0; i < 9; i++){//rows
- for(int j = 0; j < 4; j++){//columns
- if(board[i][j] == key && board[i][j+1] == key && board[i][j+2] == key && board[i][j+3] == key)
- return true;
- }//end for
- }//end for
- //vertical search
- for(int i = 0; i < 7; i++){//columns
- for(int j = 0; j < 6; j++){//rows
- if(board[j][i] == key && board[j+1][i] == key && board[j+2][i] && board[j+3][i] == key)
- return 1;
- }//end for
- }//end for
- //right slope search
- for(int i = 0; i < 6; i++){//rows
- for(int j = 0; j < 4; j++){//columns
- if(board[i][j] == key && board[i+1][j+1] == key && board[i+2][j+2] == key && board[i+3][j+3] == key)
- return 1;
- }//end for
- }//end for
- //left slope search
- for(int i = 0; i < 6; i++){//rows
- for(int j = 6; j > 2; j--){//columns
- if(key == board[i][j] && key == board[i+1][j-1] && key == board[i+2][j-2] && key == board[i+3][j-3])
- return 1;
- }//end for
- }//end for
- return 0;
- }//end linSearch
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement