Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ncurses.h>
- #include <iostream>
- #include <random>
- #include <utility>
- #include "ai.h"
- #include "game.h"
- #include <cstring>
- // The main game loop for Connect 4.
- // *** Utility functions only used in this file. ***
- // Switches from player 1 to player 2 and vice-versa.
- int SwitchPlayer(int player);
- // Get a move from the human. Updates UI to show column selection.
- bool GetPlayerMove(int board[6][7], int &move);
- // Plays a single game.
- int PlayGame(std::pair<PlayerType, PlayerType> players, bool silent);
- int main(int argc, char **argv) {
- srand(time(0));
- std::pair<PlayerType, PlayerType> players;
- if (argc == 3) {
- players = std::make_pair(GetPlayerType(argv[1]), GetPlayerType(argv[2])); // Keeping track of the amount of wins and loses
- } else if (argc == 1) {
- players = std::make_pair(PlayerType::kHuman, PlayerType::kRandom); // kHuman is one ai the other ai is kRandom
- } else {
- std::cerr << "Usage: ./connect4 [player1] [player2]" << std::endl;
- return 1;
- }
- // Show the screen if either player is a human.
- bool silent = true;
- if (players.first == PlayerType::kHuman || players.second == PlayerType::kHuman) {
- silent = false;
- initscr();
- cbreak();
- noecho();
- keypad(stdscr, TRUE);
- }
- if (silent) {
- int player1_wins = 0; int player2_wins = 0; int draws = 0;
- int winner = PlayGame(players, silent);
- if (winner == 1) { player1_wins += 1; }
- else if (winner == 2) { player2_wins += 1; }
- else if (winner == -1) { draws += 1; }
- std::cout << "Player 1 wins: " << player1_wins << std::endl;
- std::cout << "Player 2 wins: " << player2_wins << std::endl;
- std::cout << "Draws: " << draws << std::endl;
- } else {
- // Play a single game Human vs. AI.
- PlayGame(players, silent); // Inner
- }
- // Wait for any key press.
- if (!silent) {
- getch();
- endwin();
- }
- return 0;
- }
- // Returns false if the user wants to exit.
- bool GetPlayerMove(int board[6][7], int &move) {
- int ch;
- // This function goes inside DrawBoard
- while (true) {
- // User Move
- ch = getch();
- // To play the game, move the right arrow key and the left arrow key around. If you're done, press q
- // Press on the SPACE_BAR to place a piece
- if (ch == KEY_RIGHT && move <= 5) {
- move += 1;
- } else if (ch == KEY_LEFT && move > 0) {
- move -= 1;
- } else if (ch == ' ') {
- return true;
- } else if (ch == 'q') {
- // this is how you quit the game
- return false;
- }
- DrawBoard(board, move); // Inline function.
- }
- }
- int SwitchPlayer(int player) {
- if (player == 1) {return 2;}
- else if (player == 2) {return 1;}
- else {throw;}
- }
- int PlayGame(std::pair<PlayerType, PlayerType> players, bool silent) {
- int board[6][7];
- int current_player = 1;
- InitializeBoard(board);
- int player_move = 0;
- // Loop ends if the game ends or GetPlayerMove returns false.
- bool result;
- while (silent == false) {
- DrawBoard(board, player_move);
- PlayerType player_type; // This is calling the struct
- if (current_player == 1) {player_type = players.first;}
- else if (current_player == 2) {player_type = players.second;}
- // avoid updating the GUI
- int move_to_make;
- switch (player_type) {
- case PlayerType::kHuman:
- if (!GetPlayerMove(board, player_move)) {
- printw("Game over.");
- return Winner(board);
- }
- move_to_make = player_move;
- break;
- case PlayerType::kRandom:
- move_to_make = RandomAgent(board, current_player);
- break;
- case PlayerType::kFirst:
- move_to_make = FirstOpenAgent(board, current_player);
- break;
- case PlayerType::kInvalid:
- default:
- throw;
- }
- result = Drop(board, current_player, move_to_make); // Might have to add true or false to Drop.
- if (result == true) {current_player = SwitchPlayer(current_player);}
- else if (result == false) {current_player = SwitchPlayer(current_player);}
- if (Winner(board) == 1) { silent = true;}
- else if (Winner(board) == 2) {silent = true;}
- else if (Winner(board) == -1) {silent = true;}
- }
- int winner = Winner(board);
- if (silent != false) {
- if (winner == 1) {
- printw("Player 1 wins!");
- } else if (winner == 2) {
- printw("Player 2 wins!");
- } else if (winner == -1) {
- printw("Draw.");
- } else {
- printw("Game over.");
- }
- }
- return winner;
- }
Advertisement
Add Comment
Please, Sign In to add comment