Computer Science Canada Short 2 player TicTacToe program |
Author: | A.J [ Mon Nov 02, 2009 7:59 pm ] |
Post subject: | Short 2 player TicTacToe program |
The program (about 20 lines) allows two players to play Tic Tac Toe. I store the board into one number, by representing 'X' with the binary for 1, or '01', 'O' with the binary for 2, or '10', and a blank for the binary for 0, or '00'. Each square is represented by two bits. I check for wins by 'or'ing it with different winning positions. How it works is you enter a number from 1->9 (inclusive) to play a move: 1 2 3 4 5 6 7 8 9 1 represents the top left corner, 2 represents the top middle square, etc... I took out things like error checking, as I am assuming you will enter a number from 1 - 9 and that you are going to play on a square that hasn't been played on before (It is just adding 3 additional lines, anyways). |