Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public List<List<Tuple<int,int>>> Lines (int size)
- {
- List<List<Tuple<int, int>>> lines = new List<List<Tuple<int, int>>>();
- List<Tuple<int, int>> line = new List<Tuple<int, int>>();
- //Do the columns
- for (int x = 0; x < size; x++)
- {
- for (int y = 0; y < size; y++)
- {
- line.Add(new Tuple<int, int>(x,y));
- }
- lines.Add(line);
- line = new List<Tuple<int, int>>();
- }
- //Do the rows
- for (int y = 0; y < size; y++)
- {
- for (int x = 0; x < size; x++)
- {
- line.Add(new Tuple<int, int>(x, y));
- }
- lines.Add(line);
- line = new List<Tuple<int, int>>();
- }
- //Do the left diagonal
- for (int x = 0; x < size; x++)
- {
- line.Add(new Tuple<int, int>(x, x));
- }
- lines.Add(line);
- line = new List<Tuple<int, int>>();
- //Do the right diagonal
- for (int x = 0; x < size; x++)
- {
- line.Add(new Tuple<int, int>(x, size - 1 - (x % size)));
- }
- lines.Add(line);
- return lines;
- }
- public List<Player> LineAsPlayers(List<Tuple<int,int>> line, Game game)
- {
- List<Player> lineObj = new List<Player>();
- foreach (Tuple<int,int> coords in line)
- {
- lineObj.Add(game.GetLocation(coords.Item1, coords.Item2));
- }
- return lineObj;
- }
- public TicTacToeOutcome<Player> LineOutcome(List<Tuple<int, int>> line, Game game)
- {
- List<Player> listObj = LineAsPlayers(line, game);
- if (listObj.Contains(Nought) && listObj.Contains(Cross))
- {
- return TicTacToeOutcome<Player>.Draw;
- }
- if (listObj.TrueForAll((obj) => obj == Nought))
- {
- return TicTacToeOutcome<Player>.NewWin(Nought, line.AsEnumerable());
- }
- if (listObj.TrueForAll((obj) => obj == Cross))
- {
- return TicTacToeOutcome<Player>.NewWin(Cross, line.AsEnumerable());
- }
- return TicTacToeOutcome<Player>.Undecided;
- }
- public TicTacToeOutcome<Player> GameOutcome(Game game)
- {
- List<TicTacToeOutcome<Player>> boardOutcomes = new List<TicTacToeOutcome<Player>>();
- foreach (var line in Lines(game.Size))
- {
- boardOutcomes.Add(LineOutcome(line, game));
- }
- if (boardOutcomes.TrueForAll((obj) => obj == TicTacToeOutcome<Player>.Draw))
- {
- return TicTacToeOutcome<Player>.Draw;
- }
- if (boardOutcomes.TrueForAll((obj) => obj == TicTacToeOutcome<Player>.Draw || obj == TicTacToeOutcome<Player>.Undecided))
- {
- return TicTacToeOutcome<Player>.Undecided;
- }
- return boardOutcomes.Find((obj) => obj != TicTacToeOutcome<Player>.Draw && obj != TicTacToeOutcome<Player>.Undecided);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement