Well i've been trying to write code for a game of othello using Walter Savitch's game class as a parent. However I have encountered several problems that I simply haven't made much headway with over the last couple days
Code for the Othello class:
othello.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#ifndef OTHELLO_H
#define OTHELLO_H
#include "game.h"
#include "piece.h"
#include "colors.h" //changes the colors of whats printed on screen
namespace main_savitch_14
{
class othello:public game
{
public:
void restart();
void display_status() const;
bool is_legal(const string& move) const;
string get_move();
void make_move(string& move);
bool winning();
private:
piece board[8][8];
};
}
#endif
|
othello.cc:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include "othello.h"
using namespace std;
namespace main_savitch_14
{
void othello::restart()
{
board[3][3].set_white();
board[4][4].set_white();
board[3][4].set_black();
board[4][3].set_black();
}
void othello::display_status() const
{
int i;
int j;
cout << "\n\n";
cout << MAGENTA << "\n Othello Board \n";
cout << "_____________________\n";
cout << " A B C D E F G H \n";
for(i = 0; i < 8; ++i)
{
cout << MAGENTA << i + 1;
for(j = 0; j < 8; ++j)
{
cout << B_GREEN << " ";
if(board[i][j].is_black())
{
cout << BLACK << "0";
}
else if(board[i][j].is_white())
{
cout << WHITE << "0";
}
else
{
cout << " ";
}
}
cout << RESET;
cout << endl;
}
}
bool othello::is_legal(const string& move) const
{
int y = move[0];
int x = move[1];
if(next_mover() == game::HUMAN)
{
if(board[x-1][y-1].is_white())
{return true;}
if(board[x-1][y].is_white())
{return true;}
if(board[x][y-1].is_white())
{return true;}
if(board[x+1][y+1].is_white())
{return true;}
if(board[x+1][y].is_white())
{return true;}
if(board[x][y+1].is_white())
{return true;}
if(board[x-1][y+1].is_white())
{return true;}
if(board[x+1][y-1].is_white())
{return true;}
else
{return false;}
}
else if(next_mover() == game::COMPUTER)
{
if(board[x-1][y-1].is_black())
{return true;}
if(board[x-1][y].is_black())
{return true;}
if(board[x][y-1].is_black())
{return true;}
if(board[x+1][y+1].is_black())
{return true;}
if(board[x+1][y].is_black())
{return true;}
if(board[x][y+1].is_black())
{return true;}
if(board[x-1][y+1].is_black())
{return true;}
if(board[x+1][y-1].is_black())
{return true;}
else
{return false;}
}
}
// make move should be recursive but I'm having trouble figuring that out
void othello::make_move(string& move)
{
int row, column;
column = move[0];
if(column == 65 ||column == 66 || column == 67 || column == 68 || column == 69 || column == 70 || column == 71 || column == 72)
{
column = column - 65;
}
else
{
column = column - 97;
}
row = move[1] - 49;
board[row][column].set_black();
if(is_legal(move) && next_mover() == game::HUMAN)
{
if(board[row - 1][column - 1].is_white())
{board[row - 1][column - 1].flip();}
if(board[row - 1][column].is_white())
{board[row - 1][column].flip();}
if(board[row][column - 1].is_white())
{board[row][column - 1].flip();}
if(board[row + 1][column + 1].is_white())
{board[row + 1][column + 1].flip();}
if(board[row + 1][column].is_white())
{board[row + 1][column].flip();}
if(board[row][column + 1].is_white())
{board[row][column + 1].flip();}
if(board[row + 1][column - 1].is_white())
{board[row + 1][column - 1].flip();}
if(board[row - 1][column + 1].is_white())
{board[row - 1][column + 1].flip();}
}
if(is_legal(move) && next_mover() == game::COMPUTER)
{
if(board[row - 1][column - 1].is_black())
{board[row - 1][column - 1].flip();}
if(board[row - 1][column].is_black())
{board[row - 1][column].flip();}
if(board[row][column - 1].is_black())
{board[row][column - 1].flip();}
if(board[row + 1][column + 1].is_black())
{board[row + 1][column + 1].flip();}
if(board[row + 1][column].is_black())
{board[row + 1][column].flip();}
if(board[row][column + 1].is_black())
{board[row][column + 1].flip();}
if(board[row + 1][column - 1].is_black())
{board[row + 1][column - 1].flip();}
if(board[row - 1][column + 1].is_black())
{board[row - 1][column + 1].flip();}
}
}
string othello::get_move()
{
if(next_mover() == game::HUMAN)
{
cout << BLUE << "Player One's move" << endl;
string answer;
cout << "Enter your move: " << RESET;
cin >> answer;
return answer;
}
if(next_mover() == game::COMPUTER)
{
cout << RED << "Player two's move" << endl;
string answer;
cout << "Enter your move: " << RESET;
cin >> answer;
return answer;
}
}
bool othello::winning()
{
int row, column;
int whcnt, blcnt = 0;
for(row = 0; row < 8; ++row)
{
for(column = 0; column < 8; ++ column)
{
if(board[row][column].is_white())
{whcnt++;}
if(board[row][column].is_black())
{blcnt++;}
}
}
if(whcnt < blcnt)
{return game::COMPUTER;}
if(whcnt > blcnt)
{return game::HUMAN;}
if(whcnt = blcnt)
{return game::NEUTRAL;}
}
}
|
and the main.cc file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>
#include "othello.h"
#include "piece.h"
#include "game.h"
using namespace std;
using namespace main_savitch_14;
int main()
{
int i = 0;
string move;
othello dave;
dave.restart();
dave.display_status();
while(i < 8)
{
move = dave.get_move();
if(dave.is_legal(move))
{
dave.make_move(move);
dave.display_status();
++i;
}
else
{
cout << "Not a legal move, Please try again";
}
}
}
|
What is supposed to happen is a simple two player(both human / keyboard input) othello game with the second person taking the role of the computer. it's supposed to start with four peices (two white, two black) in the center and then ask for a move to be inputted from the keyboard
The problems I'm encountering are
- Numerous extra pieces on the board, no idea where they're coming from
- doesn't seem to switch over to player two so no white peices get added to the board
- make move should be recursive, but I'm having some trouble figuring out how that would work(I suspect I may be overthinking it)
I had created an image of what I want the output to be vs what I'm getting but I'm not sure how to insert it