parent
56216e904f
commit
e8b145707d
@ -0,0 +1,52 @@ |
||||
import { CurrentOutcome, GameRules, SquareState } from "../../datatypes/types.ts"; |
||||
|
||||
// Simple rules: whoever occupies all the squares passed as a parameter, wins
|
||||
// This ruleset is only used in tests currently
|
||||
export const createRulesForSquares = (...squares: [number, number][]): GameRules => ({ |
||||
getBoardOutcome: (board) => { |
||||
let hasX = false; |
||||
let hasO = false; |
||||
let hasUnoccupied = false; |
||||
|
||||
for (const [row, column] of squares) { |
||||
const state = board.get(row, column); |
||||
switch (state) { |
||||
case SquareState.X: |
||||
hasX = true; |
||||
break; |
||||
case SquareState.O: |
||||
hasO = true; |
||||
break; |
||||
case SquareState.Unoccupied: |
||||
hasUnoccupied = true; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
if (hasX && hasO) { |
||||
return CurrentOutcome.Draw; |
||||
} |
||||
|
||||
if (hasUnoccupied) { |
||||
return CurrentOutcome.Undecided; |
||||
} |
||||
|
||||
if (hasX) { |
||||
return CurrentOutcome.WinX; |
||||
} |
||||
|
||||
if (hasO) { |
||||
return CurrentOutcome.WinO; |
||||
} |
||||
|
||||
// Special case: if there are no X, O or unoccupied squares,
|
||||
// this means that the squares list is empty,
|
||||
// and in this case we only want to decide the board as Draw when it's full
|
||||
// and no moves can be made.
|
||||
if (board.serialize().includes("_")) { |
||||
return CurrentOutcome.Undecided; |
||||
} |
||||
|
||||
return CurrentOutcome.Draw; |
||||
}, |
||||
}); |
Loading…
Reference in new issue