Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- randomize who goes first
- initialize board
- print the board
- while game is not over
- ask the player to make a move
- take input of player
- drop puck in board
- printBoard
- check if won
- if won => set game to over
- if not won => switch player
- print winner
- */
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <random>
- using namespace std;
- const int HORIZONTAL = 7;
- const int VERTICAL = 6;
- int board[HORIZONTAL][VERTICAL];
- //empty = 0
- //PUCK O = 1
- //PUCK X = -1
- void initializeBoard() {
- for (int i = 0; i < 7; i++) {
- for (int j = 0; j < 6; j++) {
- board[i][j] = 0;
- }
- }
- }
- void printBoard() {
- for (int i = 0; i < 7; i++) {
- for (int j = 0; j < 6; j++) {
- if (board[i][j] == 0) {
- cout << "| ";
- }
- else if (board[i][j] == 1) {
- cout << "|O";
- }
- else if (board[i][j] == -1) {
- cout << "|X";
- }
- }
- cout << "|" << endl;
- }
- cout << endl;
- }
- //function: dropPuck()
- //input: HORIZONTAL number (int), between 0 and 6, player (1 or -1)
- //output: boolean; true if success, false if not.
- //purpose: Stores 1 (O) or -1 (X) into board[HORIZONTAL][VERTICAL] where the VERTICAL is the top of the pile in that HORIZONTAL row
- bool dropPuck(int vert, int player) {
- int i = 0;
- while ((board[i][vert] == 0) && (i < HORIZONTAL)) {
- i++;
- }
- if (i == 0) {
- return false;
- }
- board[i - 1][vert] = player;
- return true;
- }
- //function: checkWin()
- //input: N/A
- //output: true if current player wins, false if else
- /*bool checkWin() {
- for (int j = 0; i < VERTICAL; j++)
- for (int i = 0; i < HORIZONTAL; i++) {
- }
- return false;
- }
- */
- int main() {
- bool gameover = false;
- srand(time(0));
- int random = rand();
- int player = 1-2*(random%2);
- cout << "Player " << player << " is starting." << endl;
- cout << endl;
- initializeBoard();
- printBoard();
- while(!gameover) {
- cout << "Input a number from 0 to 5 to drop the puck." << endl;
- int drop_pos = 0;
- cin >> drop_pos;
- while ((drop_pos > 5) || (drop_pos < 0) || (!dropPuck(drop_pos, player))) {
- cout << endl;
- cout << "INVALID MOVE; PUCK IS OUT OF BOUNDS" << endl;
- cout << endl;
- cout << endl;
- cout << "Input a number from 0 to 5 to drop the puck." << endl;
- cin >> drop_pos;
- }
- printBoard();
- //gameover = checkWin();
- player = player *-1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement