- #1
FallArk
- 127
- 0
Main is posted below and cannot be modified. Still need to fill out the functions needed, only this time I need to use class.
What I have done(still working on it, will update as I go on):
There are a few things I don't really get, in the previous assignment I used struct to create the board, but this time how would I create a board using class?
Update:
A few more questions:
1. Do I need a private: in my class?
2. Which variable should be public? I'm thinking marker and moveNumber
3. Since I need to create a board elsewhere, how should I let the other functions use it without making it a parameter?
Code:
int main() {
int uRow = 0, uCol = 0;
int oRow = NUMROWS - 1, oCol = NUMCOLS - 1;
bool win = true;
int move = 0;
Board board;
board.initialize();
board.placePiece(uRow, uCol, 'U', move);
board.placePiece(oRow, oCol, 'X', move);
board.showBoard(move);
while (true) {
move++;
try {
movePlayer(board, uRow, uCol, move);
moveOpponent(board, oRow, oCol, move);
board.showBoard(move);
}
catch (runtime_error &excpt) {
processException(excpt, win);
break;
}
}
board.showBoard(move);
if (win)
cout << "*** YOU WIN! ***" << endl;
else
cout << "*** YOU LOSE! ***" << endl;
string tmp; getline(cin, tmp); // optional
}
What I have done(still working on it, will update as I go on):
Code:
#include <string>
#include <iostream>
#include <stdexcept>
using namespace std;
const int NUMROWS = 7;
const int NUMCOLS = 12;
const int TAILLENGTH = 3;
class Board {
public:
struct _cell {
int moveNumber;
char marker;
};
_cell board[NUMROWS][NUMCOLS];
void initialize(){
for (int r = 0; r < NUMROWS; r++) {
for (int c = 0; c < NUMCOLS; c++) {
board[r][c].marker = '_';
board[r][c].moveNumber = 0;
}
}
}
void placePiece(int currRow, int currCol, char playerPos, int move){
board[currRow][currCol].marker = playerPos;
board[currRow][currCol].moveNumber = move;
}
void showBoard(int move){
for (int r = 0; r < NUMROWS; r++) {
for (int c = 0; c < NUMCOLS; c++) {
if ((board[r][c].marker != '_') && (move - board[r][c].moveNumber > TAILLENGTH))
board[r][c].marker = '_';
cout << board[r][c].marker << " ";
}
cout << endl;
}
}
void BoardPosUser(int currRow, int currCol, int move) {
if ((currRow < 0) || (currRow > NUMROWS) || (currCol < 0) || (currCol > NUMCOLS)) {
throw runtime_error("YOU FELL OFF THE BOARD!");
}
if (board[currRow][currCol].marker != '_') {
throw runtime_error("BANG! You're dead!");
}
}
void BoardPosOppo(int &oRow, int &oCol, int move){
if ((oRow > 0) && (board[oRow - 1][oCol].marker == '_')) {
oRow--;
placePiece(oRow, oCol, 'X', move);
}
if ((oRow < NUMROWS-1) && (board[oRow + 1][oCol].marker == '_')) {
oRow++;
placePiece(oRow, oCol, 'X', move);
}
if ((oCol > 0) && (board[oRow][oCol-1].marker == '_')) {
oCol--;
placePiece(oRow, oCol, 'X', move);
}
if ((oCol < NUMCOLS-1) && (board[oRow][oCol+1].marker == '_')) {
oCol++;
placePiece(oRow, oCol, 'X', move);
}
}
};void movePlayer(Board board, int &currRow, int &currCol, int move) {
board.placePiece(currRow, currCol, 'u', move);
cout << "Enter direction (N/S/E/W): ";
char direction;
cin >> direction;
if (direction == 'N')
currRow--;
if (direction == 'S')
currRow++;
if (direction == 'W')
currCol--;
if (direction == 'E')
currCol++;
board.BoardPosUser(currRow, currCol, move);
board.placePiece(currRow, currCol, 'U', move);
}
void moveOpponent(Board board, int &oRow, int &oCol, int move) {
board.placePiece(oRow, oCol, 'x', move);
board.BoardPosOppo(oRow, oCol, move);
throw runtime_error("OPPONENT LOSES");
}
void processException(runtime_error excpt, bool win){
win = (excpt.what() == string("OPPONENT LOSES"));
}
A few more questions:
1. Do I need a private: in my class?
2. Which variable should be public? I'm thinking marker and moveNumber
3. Since I need to create a board elsewhere, how should I let the other functions use it without making it a parameter?
Last edited: