python-ai / src / othello / Game.cpp

#include "Game.hpp"
#include <iostream>

using std::cout;
using std::endl;

void Game::play() const {
    const string fstPlayerName = fst->toString() + " (black)";
    const string sndPlayerName = snd->toString() + " (white)";
    Board board;
    BoardPlayerNum turn = FST_PLAYER;
    while(!board.complete()) {
        const BoardPlayerNum currentNum = turn;
        const BoardPlayerNum otherNum = Board::otherPlayer(turn);
        Player * const current = (turn == FST_PLAYER) ? fst : snd;
        Player * const other = (turn == FST_PLAYER) ? snd : fst;
        const BoardLocs validMoves = board.validMoves(currentNum);
        BoardLoc move;
        
        cout << board.toString();
        cout << "Current player: ";
        cout << ((turn == FST_PLAYER) ? fstPlayerName : sndPlayerName);
        cout << endl;
        
        if(validMoves.size() == 0) {
            cout << "No moves available. Skipping turn." << endl;
            turn = otherNum;
            continue;
        }
        
        while(true) {
            move = current->chooseMove(board, validMoves);
            if (Board::moveValid(move, validMoves)) break;
            cout << "Invalid move " << Board::boardLocToString(move) << ". ";
            cout << " Please try again." << endl;
        }
        board = board.makeMove(move, currentNum);
        turn = otherNum;
    }
    
    const BoardPlayerNum winnerNum = board.winner();
    const BoardPlayerNum loserNum = Board::otherPlayer(winnerNum);
    Player * const winner = (winnerNum == FST_PLAYER) ? fst : snd;
    Player * const loser = (winnerNum == FST_PLAYER) ? snd : fst;
    
    cout << board.toString();
    cout << endl << "Final Score" << endl;
    cout << "===========" << endl;
    cout << ((winnerNum == FST_PLAYER) ? fstPlayerName : sndPlayerName);
    cout << ": " << (int)board.score(winnerNum) << endl;
    cout << ((winnerNum == FST_PLAYER) ? sndPlayerName : fstPlayerName);
    cout << ": " << (int)board.score(loserNum) << endl;
}

int main(int argc, char **argv) {
    NaivePlayer fst(FST_PLAYER);
    MinimaxPlayer snd(SND_PLAYER);
    const Game game(&fst, &snd);
    game.play();
    return 0;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.