📄 gameengine.cs
字号:
using System;
using System.Collections.Generic;
namespace TicTacToe
{
/// <summary>
/// Tic Tac Toe game engine
/// </summary>
public class GameEngine : ITicTacToe
{
public GameEngine()
{
cells = new CellState[numberOfCells];
// Seed random number generator with current time
random = new Random(unchecked((int)DateTime.Now.Ticks));
level = Level.Hard;
NewGame();
}
/// <summary>
/// Set difficulty level of computer opponent games
/// </summary>
/// <param name="level">Difficulty level</param>
public void SetLevel(Level level)
{
this.level = level;
}
/// <summary>
/// Start a new game
/// </summary>
public void NewGame()
{
crossesTurn = true;
cellsFree = numberOfCells;
winner = CellState.Empty;
for (int i = 0; i < numberOfCells; i++)
cells[i] = CellState.Empty;
}
public bool IsCellEmpty(int index)
{
return (cells[index] == CellState.Empty);
}
public CellState GetCellState(int index)
{
return cells[index];
}
/// <summary>
/// Is it crosses turn?
/// </summary>
/// <returns>True if it is crosses turn to make a move</returns>
public bool IsCrossesTurn()
{
return crossesTurn;
}
/// <summary>
/// Occupy cell by placing a symbol in the cell
/// </summary>
/// <param name="index">Zero-based cell index</param>
public void OccupyCell(int index)
{
if (cells[index] == CellState.Empty)
cellsFree--;
if (crossesTurn)
cells[index] = CellState.Cross;
else
cells[index] = CellState.Circle;
crossesTurn = !crossesTurn;
}
/// <summary>
/// Make a computer generated move.
/// NB: Computer always plays circle (red)
/// </summary>
public void MakeComputerMove()
{
int index = -1;
List<int> freeCells = new List<int>(cellsFree);
// Make collection of free cells
for (int i=0; i < cells.Length; i++)
{
if (cells[i] == CellState.Empty)
freeCells.Add(i);
}
if (freeCells.Count == 1)
{
index = freeCells[0];
}
else if (freeCells.Count > 1)
{
if (level == Level.Hard)
{
// For each free cell check if we can win by occupying it
for (int i = 0; i < freeCells.Count && index == -1; i++)
{
cells[freeCells[i]] = CellState.Circle;
if (IsGameOver())
index = freeCells[i];
cells[freeCells[i]] = CellState.Empty;
}
// For each free cell check if we can prevent opponent from
// winning by occupying it
for (int i = 0; i < freeCells.Count && index == -1; i++)
{
cells[freeCells[i]] = CellState.Cross;
if (IsGameOver())
index = freeCells[i];
cells[freeCells[i]] = CellState.Empty;
}
}
if (index == -1)
{
// No obvious moves, pick a random cell
index = freeCells[random.Next(freeCells.Count)];
}
}
if (index >= 0)
OccupyCell(index);
}
public CellState GetWinner()
{
return winner;
}
public bool IsGameOver()
{
winner = CellState.Empty;
// Check for all winner patterns
if (cells[0] != CellState.Empty && cells[0] == cells[1] && cells[1] == cells[2])
{
winner = cells[0];
return true;
}
if (cells[3] != CellState.Empty && cells[3] == cells[4] && cells[4] == cells[5])
{
winner = cells[3];
return true;
}
if (cells[6] != CellState.Empty && cells[6] == cells[7] && cells[7] == cells[8])
{
winner = cells[6];
return true;
}
if (cells[0] != CellState.Empty && cells[0] == cells[3] && cells[3] == cells[6])
{
winner = cells[0];
return true;
}
if (cells[1] != CellState.Empty && cells[1] == cells[4] && cells[4] == cells[7])
{
winner = cells[1];
return true;
}
if (cells[2] != CellState.Empty && cells[2] == cells[5] && cells[5] == cells[8])
{
winner = cells[2];
return true;
}
if (cells[0] != CellState.Empty && cells[0] == cells[4] && cells[4] == cells[8])
{
winner = cells[0];
return true;
}
if (cells[2] != CellState.Empty && cells[2] == cells[4] && cells[4] == cells[6])
{
winner = cells[2];
return true;
}
if (cellsFree == 0)
return true;
return false;
}
public int Rows
{
get { return numberOfRows; }
}
public int Columns
{
get { return numberOfColumns; }
}
public int Cells
{
get { return numberOfCells; }
}
private const int numberOfColumns = 3;
private const int numberOfRows = 3;
private const int numberOfCells = numberOfColumns * numberOfRows;
private int cellsFree;
private bool crossesTurn;
private Level level;
private Random random;
private CellState winner;
private CellState[] cells;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -