// nothing more than connect four // (poet and dont know it) #include #include // function initalizes the board by filling it with periods as placeholders void initialize(char board[6][7]){ int i, j; for(i = 0; i < 6; i++) for(j = 0; j < 7; j++) board[i][j] = '.'; } // function prints the board to the screen. nothing complex. void draw_board(char board[6][7]){ int i, j; system("cls"); cout << " | 1 | 2 | 3 | 4 | 5 | 6 | 7 | " << endl << endl; for(i = 5; i >= 0; i--){ cout << " | "; for(j = 0; j < 7; j++) cout << board[i][j] << " | "; cout << endl; } cout << endl; } // function tests for victory on either side with all eight directions bool detect_win(char board[6][7], int x, int y){ int win = 0, ocol = x, orow = y; int maxy = 5, maxx = 6, min = 0; // essential for neatness // board is in (y,x) format, as compared to (x,y) standard char p = board[y][x]; //////////////////////////////////////////////////////////////////////////// // essentially the program is just finding the starting point of a winning // line, and counting on from there. the best way to do this is by finding // which directions are similar, and follow them through. i'm not sure // how i would implement this yet, however, i think i can just use a couple // more variables for direction, maybe an array to hold the directions. i // could cycle through the directions, find the starting point, and count // the winning pieces until i get to four of them. //////////////////////////////////////////////////////////////////////////// // tests horozontal victory///////////////////////////////////////////////// while(p == board[y][x-1] && x - 1 >= min && win == 0) // x--; // while(p == board[y][x+1] && x + 1 <= maxx && win < 3){ // x++; // win++; // } // if(win >= 3) return true; // exit for victory // //////////////////////////////////////////////////////////////////////////// // resets values win = 0; x = ocol; y = orow; // tests vertical victory/////////////////////////////////////////////////// while(p == board[y-1][x] && y - 1 >= min && win == 0) // y--; // while(p == board[y+1][x] && y + 1 <= maxy && win < 3){ // y++; // win++; // } // if(win >= 3) return true; // exit for victory // //////////////////////////////////////////////////////////////////////////// // resets values win = 0; x = ocol; y = orow; //tests negative-slope diagonal victory///////////////////////////////////// while(p == board[y+1][x-1] && y + 1 <= maxy && x - 1 <= min && win == 0){ // y++; // x--; // } // while(p == board[y-1][x+1] && y - 1 >= min && x + 1 <= maxx && win < 3){ // y--; // x++; // win++; // } // if(win >= 3) return true; // exit for victory // //////////////////////////////////////////////////////////////////////////// // resets values win = 0; x = ocol; y = orow; // tests positive-slope diagonal victory//////////////////////////////////// while(p == board[y-1][x-1] && y - 1 >= min && x - 1 >= min && win == 0){ // y--; // x--; // } // while(p == board[y+1][x+1] && y + 1 <= maxy && x + 1 <= maxx && win < 3){ // y++; // x++; // win++; // } // if(win >= 3) return true; // exit for victory // //////////////////////////////////////////////////////////////////////////// // when they all fail, function returns false return false; } // function validates placement input bool check_col(char board[6][7], char turn, int col, int &row){ int i = 0; // makes sure that user picks a valid column if(col >= 0 && col < 7){ // finds row number of open slot while(board[i][col] != '.') i++; // if avalible, place piece in slot if(i < 7){ board[i][col] = turn; row = i; return true; } } // when validity fails cout << "Invalid column. Please pick another column" << endl; return false; } // main game loop. void play(){ int turn = 0, row; char p, board[6][7], colors[2] = {'x','y'}; bool done = false; initialize(board); // main while loop. will loop until a victory or the board is full do{ int col; draw_board(board); // input while loop. verifies input do{ cout << "Player " << (turn % 2) + 1 << ", pick a column: "; cin >> col; } while(!check_col(board, colors[turn % 2], col - 1, row)); // checks for win if(detect_win(board, row, col - 1)) done = true; else turn++; } while(!done && turn < 42); // prints final board draw_board(board); // if a victory, else a tie if(done) cout << "Player " << turn % 2 + 1 << ", You won!" << endl; else cout << "Tie game, good job both players" << endl; // standard "play again" input cout << "Would you like to play again? (y/n)" << endl; cin >> p; if(p == 'y' || p == 'Y') play(); else cout << "Thanks for playing!" << endl; } // main function is main int main(){ play(); return 0; }