Microsoft Interview Question
Software Engineer / DevelopersSome additions:
- initialize winner to FALSE
- initialize counter - to zero. when the counter increases to 9, the board is full = the while loop condition
- initialization of (-20) of the array
- Array of integers when player A assigns 1 and player 2 assigns 0
- when checking for win - sum up and check whether the value is 3 or 0
- switch to deferentaite the path that should be checked for each position. the corners requires 3 checks while the other positions require 2 checks
- the game can end without winner
// input board size and number x;
- James May 10, 2006Use 2D array that is allocated dynamically;
declare an array board;
init board to all 0;
begin loop
get the input into pos
if from player 1, set 1 at pos on board
else if from play 2, set 2 at pos on board
check row on pos;
check column on pos;
check two diagonals on pos;
if any one of them contains x number consecutive 1 or 2;
corresponding player win and restart game
else if all pos on board are occupied, tie and restart game
else continue loop
end loop