Skip to content

Instantly share code, notes, and snippets.

@andrewfhart
Created November 16, 2015 04:28
Show Gist options
  • Save andrewfhart/d2210f0b60cb3f344807 to your computer and use it in GitHub Desktop.
Save andrewfhart/d2210f0b60cb3f344807 to your computer and use it in GitHub Desktop.
Tic-tac-toe Tutorial (Step 1)
#include <iostream>
using namespace std;
/**
* Draw a tic-tac-toe board with row and column labels
* The cell data in board will be interpreted as follows:
* 1 = owned by 'x'
* 2 = owned by 'o'
* all other values = empty
*
* @param int[3][3] board The current state of each board cell
* @return void
*/
void drawBoard(int board[][3]) {
cout << " " << " A B C " << endl;
cout << " " << "+---+---+---+" << endl;
for (int row = 0; row < 3; row++) {
cout << row << " ";
for (int col = 0; col < 3; col++) {
switch (board[row][col]) {
case 1: cout << "| " << "x" << " "; break;
case 2: cout << "| " << "o" << " "; break;
default:cout << "| " << " " << " "; break;
}
}
cout << "|" << endl;
cout << " " << " " << "+---+---+---+" << endl;
}
}
int main (int argc, char* argv[])
{
// State variables
//
// Board:
// 0 1 2
// +---+---+---+
// 0 | | | x | x = board[0][2]
// +---+---+---+
// 1 | | | |
// +---+---+---+
// 2 | | | |
// +---+---+---+
//
// Each board square will be in one of three possible
// states: 0=empty, 1=owned by 'x', 2=owned by 'o'.
int board[3][3] = {};
// Let's test our board
board[0][0] = 1;
board[1][1] = 1;
board[2][2] = 1;
board[0][2] = 2;
board[2][0] = 2;
drawBoard(board);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment