Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- const int a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,black=1,white=2,suggestion=10;
- const string space = "\t ";
- //Othello challenge
- //Copyright: Federico Rampazzo, 2011
- int followLine(int grid[9][9], int i, int j, int vi, int vj,
- int color, int replace, bool target=true,
- int limit=0, int captured=0){
- //follow a line of color pieces using the vectors vi e vj starting from i,j
- //target==true: place a replace piece as soon as it founds a limit piece
- //target==false: place replace pieces along the line until it founds a limit piece
- //captured holds the number of captured pieces
- if (i>=1 && i<=8 && j>=1 && j<=8){
- if (grid[i][j]==color){
- int result = followLine(grid, i+vi, j+vj, vi, vj, color, replace, target, limit, captured+1);
- if (!target && result) grid[i][j] = replace;
- return result;
- }else if(grid[i][j]==limit && captured>0){
- if (target) grid[i][j] = replace;
- return 1;
- }else{
- return 0;
- }
- }
- return 0;
- }
- int showValidMoves(int grid[9][9], int color){ //display suggestions
- int opposite = ((color==black) ? white : black);
- int moves = 0;
- for (int i=1; i<=8; i++)
- for (int j=1; j<=8; j++)
- if (grid[i][j]==color)
- for (int k=-1; k<=1; k++)
- for (int h=-1; h<=1; h++)
- moves += followLine(grid, i+k, j+h, k, h, opposite, suggestion);
- return moves;
- }
- void cleanSuggestions(int grid[9][9]){ //clean the grid
- for (int i=1; i<=8; i++){
- for (int j=1; j<=8; j++){
- if (grid[i][j]==suggestion)
- grid[i][j] = 0;
- }
- }
- }
- int getWinner(int grid[9][9], int &blacks, int &whites){ //calculate the winner
- for (int i=1; i<=8; i++){
- for (int j=1; j<=8; j++){
- if (grid[i][j]==black)
- blacks++;
- else if (grid[i][j]==white)
- whites++;
- }
- }
- if (blacks>whites)
- return black;
- else if (blacks<whites)
- return white;
- else
- return 0;
- }
- void printGrid(int grid[9][9]){ //print the grid
- cout << space;
- cout << " a b c d e f g h\n";
- for (int i=1; i<=8; i++){
- cout << space;
- cout << i << " ";
- for (int j=1; j<=8; j++){
- cout << ((grid[i][j]==white) ? "\u25CF" :
- ((grid[i][j]==black) ? "\u25CB" :
- ((grid[i][j]==suggestion) ? "\u00B7" :
- " "))) << " ";
- }
- cout << i << "\n";
- }
- cout << space;
- cout << " a b c d e f g h\n";
- }
- int main(){
- int grid[9][9]={};
- grid[4][d] = grid[5][e] = white;
- grid[5][d] = grid[4][e] = black;
- int color = black; //black is first
- int consecutive = 0;
- bool legal = showValidMoves(grid, color)>0;
- while (legal || consecutive<2){ //2 stalls make the game end
- string player = ((color==black) ? "BLACK" : "WHITE");
- int opposite = ((color==black) ? white : black);
- printGrid(grid);
- if (!legal){
- consecutive++;
- cout << space << "No valid moves for " << player << "\n";
- }else{ //user input
- consecutive = 0;
- bool enteredMove = false;
- while (!enteredMove){
- cout << space << "It's " << player << "'s turn: ";
- string move; getline(cin, move);
- int i = (int) move[1]-48, j = (int) tolower(move[0])-96;
- if (grid[i][j]==suggestion){
- enteredMove = true;
- grid[i][j] = color;
- for (int k=-1; k<=1; k++)
- for (int h=-1; h<=1; h++)
- followLine(grid, i+k, j+h, k, h, opposite, color, false, color);
- }else{
- cout << space << "Unvalid move\n";
- }
- }
- cleanSuggestions(grid);
- }
- color = opposite;
- legal = showValidMoves(grid, color)>0;
- }
- //game over
- int blacks = 0, whites = 0;
- int winner = getWinner(grid, blacks, whites);
- cout << space;
- cout << ((winner==black) ? "BLACK" :
- ((winner==white) ? "WHITE" :
- "NOBODY")) << " wins! " <<
- blacks << "-" << whites << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement