-
Notifications
You must be signed in to change notification settings - Fork 1
/
gameboard.cpp
125 lines (114 loc) · 3.31 KB
/
gameboard.cpp
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
/* Written by: Mikael Jakhelln, HIOA-studentnr: s169961
Tic-Tac-Toe, n pieces on a row to win, with n sized gameboard.
*/
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
namespace tictactoe
{
class gameboard
{
public: //was originally one big file, rewrote to several classes for easier reading, hence everything is public :/
int size; //gameboard size
vector< vector<char> > board; //2D array(vector) for gameboard
//constructor
gameboard(int s)
{
size = s;
resetboard();
}
void resetboard()
{ //resets the gameboard
board.resize(size);
for(int i=0; i<size; i++)
{
board[i].resize(size);
for(int j=0; j<size; j++)
{
board[i][j] = ' ';
}
}
}
void printboardinfo()
{ //print out the gameboard, with explanations
std::string indent= "\t";
std::string row = "row";
cout << indent;for(int j = 0; j<size; j++){cout << "___";} cout << endl;
cout << "Columns:";for(int j=0; j<size; j++){cout << '|'<< j+1;}cout << '|' << endl;
cout << indent;for(int j = 0; j<size; j++){cout << "+-";} cout << "+" << endl;
for(int i=0; i<size; i++)
{
cout << row << i+1 << indent;for(int j=0; j<size; j++){cout << '|'<< board[i][j];}cout << '|'<< endl;
cout << indent; for(int j = 0; j<size; j++){cout << "+-";} cout << "+" << endl;
}
for(int j = 0; j<size; j++){cout << "____";} cout << endl;
}
void printboard()
{ //print out the current state of gameboard
for(int j = 0; j<size; j++){cout << "___";} cout << endl;
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
cout << '|'<< board[i][j];
}
cout << '|'<< endl;
for(int j = 0; j<size; j++){cout << "+-";} cout << "+" << endl;
}
for(int j = 0; j<size; j++){cout << "___";} cout << endl;
}
bool isempty() //not used, should have implemented this here from the start :/
{
//checks wether the gameboard is empty or now
for(int x=0; x<size; x++)
{
for(int y=0; y<size; y++)
{
if(board[x][y] != ' ') //if anything is on the board, return false
return false;
}
}
return true; //the board is empty
}
bool makemove(int player, int x, int y)
{ //place players gamepiece on the board
//parameters are player: 1/2, rownumber: x, columnnumber: y
x--; y--; //because index starts at 0
if(board[x][y] != ' ') //if the spot is filled, return false
{
return false;
}
if(board[x][y] == ' ') //if the spot is free, place gamepiece
{
if(player == 1){board[x][y] = 'X'; return true;}
if(player == 2){board[x][y] = 'O'; return true;}
}
return false; //if u got here, something went terribly wrong! might aswell return false
}
};//end of classdefinition gameboard
}//end of namespace tictactoe
//testmain
/*
using namespace tictactoe;
int main()
{
gameboard gameboard1 = gameboard(3);
gameboard1.printboard();
bool okmove = gameboard1.makemove(1,1,1);
cout << "1,1,1: "<<okmove << endl;
okmove = gameboard1.makemove(1,1,1);
cout << "1,1,1: "<<okmove << endl;
gameboard1.makemove(2,1,2);
gameboard1.printboard();
gameboard1.makemove(1,2,1);
gameboard1.printboard();
gameboard1.makemove(2,2,2);
gameboard1.printboard();
gameboard1.makemove(1,3,1);
gameboard1.printboard();
gameboard1.makemove(2,3,2);
gameboard1.printboard();
//everything works, onto next class
}
*/