Last active
March 10, 2016 12:01
-
-
Save battermann/aa39de5b48bc8a31cfa2 to your computer and use it in GitHub Desktop.
Model for Tic-Tac-Toe
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
using System.Collections.Generic; | |
namespace TicTacToe | |
{ | |
public enum Player | |
{ | |
PlayerX, | |
PlayerO | |
} | |
public enum GameResult | |
{ | |
PlayerXWon, | |
PlayerOWon, | |
Draw, | |
OnGoing | |
} | |
public enum HorizPos { Left, Center, Right } | |
public enum VertPos { Top, Center, Bottom } | |
public class Move | |
{ | |
public Move(VertPos vertPos, HorizPos horizPos, Player player) | |
{ | |
Player = player; | |
VertPos = vertPos; | |
HorizPos = horizPos; | |
} | |
public Player Player { get; private set; } | |
public HorizPos HorizPos { get; private set; } | |
public VertPos VertPos { get; private set; } | |
} | |
public class GameState | |
{ | |
public GameResult GameResult { get; private set; } | |
public IEnumerable<Move> Moves { get; private set; } | |
private GameState() { } | |
public static GameState Initial() | |
{ | |
return new GameState { GameResult = GameResult.OnGoing, Moves = new List<Move>() }; | |
} | |
public GameState MakeMove(Move move) | |
{ | |
// implement logic | |
return new GameState(); | |
} | |
} | |
public static class Usage | |
{ | |
public static void Run() | |
{ | |
var game = GameState.Initial(); | |
// start game loop | |
var move = new Move(VertPos.Top, HorizPos.Left, Player.PlayerX); | |
var moveResult = game.MakeMove(move); | |
switch (moveResult.GameResult) | |
{ | |
case GameResult.OnGoing: // todo | |
case GameResult.Draw: // todo | |
case GameResult.PlayerXWon: // todo | |
case GameResult.PlayerOWon: // todo | |
default: // todo | |
} | |
// ... | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment