Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
#include <iostream>
Board::Board()
{
boardSize = 0;
}
Board::~Board()
{
}
void Board::reset_board_size(unsigned int size)
{
boardSize = size;
turn = true;
//fill the board with blank spaces
for(int i = 0; i < boardSize * boardSize; i++){
b.push_back(Space::BLANK);
}
}
bool Board::make_move(unsigned int row, unsigned int column)
{
int index = (row * boardSize) + column;
if(index > b.size() || b[index] != Space::BLANK){
return false;
}
else {
if(turn){
b[index] = Space::X;
turn = false;
return true;
}
else {
b[index] = Space::O;
turn = true;
return true;
}
}
}
GameStatus Board::get_status() const
{
// check diagonals
int lxDia = 0;
int loDia = 0;
int rxDia = 0;
int roDia = 0;
for(int l = 0; l < boardSize; l++){
if(b[boardSize + 1 * l] == Space::X){
lxDia++;
}
else if(b[boardSize + 1 * l] == Space::O){
loDia++;
}
}
for(int r = 1; r <= boardSize; r++){
if(b[boardSize - 1 * r] == Space::X){
rxDia++;
}
else if(b[boardSize - 1 * r] == Space:O){
roDia++;
}
}
if(rxDia == boardSize || lxDia == boardSize){
return GameStatus:OVER_X_WON;
}
else if(roDia == boardSize || loDia == boardSize){
return GameStatus:OVER_O_WON;
}
// check each row and column
int xRow = 0;
int oRow = 0;
int xCol = 0;
int oCol = 0;
// each row
for(int i = 0; i < boardSize; i++){
// column
for(int j = 0; j < boardSize; j++){
// check rows
if(b[i * boardSize + j] == Space::X){
xRow++;
}
else if(b[i * boardSize + j] == Space::O){
oRow++;
}
// check columns
if(b[i + j * boardSize] == Space::X){
xCol++;
}
else if(b[i + j * boardSize] == Space::O){
oCol++;
}
}
// check + reset after each pass
if(xRow == boardSize || xCol == boardSize){
return GameStatus::OVER_X_WON;
}
else if(oRow == boardSize || oRow == boardSize){
return GameStatus::OVER_O_WON;
}
else{
xRow = 0;
oRow = 0;
xCol = 0;
oCol = 0;
}
}
// check for empty Space
bool hasEmpty = false;
for(int e = 0; e < b.size(); e++){
if(b[e] == Space::BLANK){
hasEmpty = true;
break;
}
}
if(!hasEmpty){
return GameStatus::OVER_TIE;
}
// if none of those conditions passed, keep playing
if(turn){
return GameStatus::PLAYING_X_TURN;
}
else{
return GameStatus::PLAYING_O_TURN;
}
}
void Board::print_board() const
{
int rowLength = (boardSize * 2) - 1;
// each row
for(int i = 0; i < boardSize; i++){
// each column
for(int j = 0; j < boardSize; j++){
switch(b[(i * boardSize) + j]){
case Space::X : std::cout << "X";
break;
case Space::O : std::cout << "O";
break;
default : std::cout << " ";
break;
}
if(j + 1 != boardSize){
std::cout << "|";
}
}
std::cout << std::endl;
if(i + 1 != boardSize){
for(int k = 0; k < rowLength; k++){
std::cout << "-";
}
}
std::cout << std::endl;
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team