Created
November 16, 2015 04:28
-
-
Save andrewfhart/d2210f0b60cb3f344807 to your computer and use it in GitHub Desktop.
Tic-tac-toe Tutorial (Step 1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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