Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Game Board for Reversilation, including initialization, update, game rules implementation, reset, win condition check, etc.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Board : MonoBehaviour {
public GameObject piece;
public Camera camera;
public Collider coll;
//private int[,] boardState;
private GameObject[,] boardPieces;
private int whiteCount;
private int blackCount;
public Text blackText;
public Text whiteText;
public Text gameoverText;
public Text restartText;
public Text exitText;
public Text returnText;
public Text displayDiff;
public Text turnText;
public List<Vector2> movesListPlayer;
public List<Vector2> movesListAI;
public List<Vector2> movesList;
private int difficulty;
private bool setDiff;
public string difficultyText;
private int emptyLeft;
public AIReversi AI;
public AudioClip win;
public AudioClip lose;
public AudioClip bgm;
public AudioClip bgm2;
public AudioClip bgm3;
public AudioClip bgm4;
private AudioSource winAudio;
private AudioSource loseAudio;
private AudioSource bgmAudio;
private AudioSource bgmAudio2;
private AudioSource bgmAudio3;
private AudioSource bgmAudio4;
private AudioSource[] bgmAudios;
private int bgmIndex;
// trigger for whether the sound is already played
private bool soundPlayed;
// function to add audio clip to audio source
public AudioSource AddAudio(AudioClip clip, bool loop, bool playAwake, float vol)
{
AudioSource newAudio = gameObject.AddComponent<AudioSource> ();
newAudio.clip = clip;
newAudio.loop = loop;
newAudio.playOnAwake = playAwake;
newAudio.volume = vol;
return newAudio;
}
void Awake()
{
bgmIndex = Random.Range (0, 4);
bgmAudios = new AudioSource[4];
bgmAudio = AddAudio (bgm, true, false, 1.0f);
bgmAudio2 = AddAudio (bgm2, true, false, 1.0f);
bgmAudio3 = AddAudio (bgm3, true, false, 1.0f);
bgmAudio4 = AddAudio (bgm4, true, false, 1.0f);
winAudio = AddAudio (win, false, false, 1.0f);
loseAudio = AddAudio (lose, false, false, 1.0f);
bgmAudios [0] = bgmAudio;
bgmAudios [1] = bgmAudio2;
bgmAudios [2] = bgmAudio3;
bgmAudios [3] = bgmAudio4;
// randomly play one audio from audio source list
bgmAudios [bgmIndex].Play ();
soundPlayed = false;
AI = new AIReversi ();
}
// Use this for initialization
void Start ()
{
coll = GetComponent<Collider> ();
movesList = new List<Vector2> ();
movesListPlayer = new List<Vector2> ();
movesListAI = new List<Vector2> ();
ResetBoard ();
restartText.text = "";
exitText.text = "";
gameoverText.text = "";
returnText.text = "";
turnText.text = "Turn: White";
}
// Update is called once per frame
void Update ()
{
SetUpPiece ();
CheckForWin ();
}
// Final Version: Players take turns to set up pieces. If a grid is containing a piece, neither of two players can set piece on that grid.
// Game rules and AI implementation complete.
void SetUpPiece()
{
// current game condition is that white will play first, which is exactly opposite to the official Othello color priority.
if (AI.gameController.colorTurn == 1) {
if (movesList.Count != 0) {
if (Input.GetButtonDown ("Fire1")) {
RaycastHit hit;
Ray ray = camera.ScreenPointToRay (Input.mousePosition);
if (coll.Raycast (ray, out hit, 100.0f)) {
float coordx = hit.point.x;
float coordz = hit.point.z;
int wholex = (int)coordx;
int wholez = (int)coordz;
float decimalx = coordx - wholex;
float decimalz = coordz - wholez;
// white player (human)
if (coordx < 7.5f && coordx > -0.5f && coordz < 7.5f && coordz > -0.5f) {
if (decimalx > 0.5f && decimalz > 0.5f) {
if (AI.gameController.IsValidMove (AI.gameController.boardState, 1, wholex + 1, wholez + 1)) {
MakeMove (AI.gameController.boardState, wholex + 1, wholez + 1);
}
}
if (decimalx < 0.5f && decimalz > 0.5f) {
if (AI.gameController.IsValidMove (AI.gameController.boardState, 1, wholex, wholez + 1)) {
MakeMove (AI.gameController.boardState, wholex, wholez + 1);
}
}
if (decimalx > 0.5f && decimalz < 0.5f) {
if (AI.gameController.IsValidMove (AI.gameController.boardState, 1, wholex + 1, wholez)) {
MakeMove (AI.gameController.boardState, wholex + 1, wholez);
}
}
if (decimalx < 0.5f && decimalz < 0.5f) {
if (AI.gameController.IsValidMove (AI.gameController.boardState, 1, wholex, wholez)) {
MakeMove (AI.gameController.boardState, wholex, wholez);
}
}
}
}
}
} else {
AI.gameController.colorTurn = 2;
movesList = AI.gameController.MoveList (AI.gameController.boardState, 2);
movesListAI = AI.gameController.MoveList (AI.gameController.boardState, 2);
UpdateTurn ();
}
} else if (AI.gameController.colorTurn == 2) {
// black player (AI)
if (movesList.Count != 0) {
// for both testing and realistic situation, please avoid using depth that is greater than 5. Otherwise
// it will take time. It will take TIME.
// Even with Alpha-Beta pruning, the amount of time it takes from 5 to 6 increases significantly.
//MoveStatus ms = AI.MiniMaxAlphaBeta (AI.gameController.boardState, 2, 3, 0, int.MinValue, int.MaxValue);
if (movesList.Count == 1) {
MakeMove (AI.gameController.boardState, (int)movesList [0].x, (int)movesList [0].y);
} else {
MoveStatus ms = AI.MiniMaxAlphaBeta (AI.gameController.boardState, 2, difficulty, 0, int.MinValue, int.MaxValue);
Debug.Log (ms.row + ", " + ms.col);
MakeMove (AI.gameController.boardState, ms.row, ms.col);
}
} else {
AI.gameController.colorTurn = 1;
movesList = AI.gameController.MoveList (AI.gameController.boardState, 1);
movesListPlayer = AI.gameController.MoveList (AI.gameController.boardState, 1);
UpdateTurn ();
}
} else
return;
}
// Reset board to initial condition.
void ResetBoard()
{
whiteCount = 2;
blackCount = 2;
emptyLeft = 60;
// default difficulty
difficulty = 3;
difficultyText = ContentChange.difficulty;
setDiff = int.TryParse (difficultyText, out difficulty);
if (setDiff) {
if (difficulty < 1)
difficulty = 1;
if (difficulty > 10)
difficulty = 10;
} else {
// default difficulty
difficulty = 3;
}
AI.gameController.colorTurn = 1;
AI.gameController.InitBoard ();
boardPieces = new GameObject[,] {
{ null, null, null, null, null, null, null, null },
{ null, null, null, null, null, null, null, null },
{ null, null, null, null, null, null, null, null },
{ null, null, null, null, null, null, null, null },
{ null, null, null, null, null, null, null, null },
{ null, null, null, null, null, null, null, null },
{ null, null, null, null, null, null, null, null },
{ null, null, null, null, null, null, null, null },
};
GameObject init;
init = Instantiate (piece, new Vector3 (3f, 1f, 4f), Quaternion.identity) as GameObject;
boardPieces[3, 4] = init;
init = Instantiate (piece, new Vector3 (4f, 1f, 3f), Quaternion.identity) as GameObject;
boardPieces[4, 3] = init;
init = Instantiate (piece, new Vector3 (3f, 1f, 3f), Quaternion.Euler(0f, 0f, 180f)) as GameObject;
boardPieces[3, 3] = init;
init = Instantiate (piece, new Vector3 (4f, 1f, 4f), Quaternion.Euler(0f, 0f, 180f)) as GameObject;
boardPieces[4, 4] = init;
movesList = AI.gameController.MoveList (AI.gameController.boardState, 1);
UpdateText ();
}
// update turn text
void UpdateTurn()
{
if (!AI.gameController.IsGameOver ()) {
if (AI.gameController.colorTurn == 1)
turnText.text = "Turn: White";
else if (AI.gameController.colorTurn == 2)
turnText.text = "Turn: Black";
}
else
turnText.text = "Game Over";
}
// update piece count and difficulty text
void UpdateText()
{
blackText.text = "Black (AI) Pieces: " + blackCount.ToString ();
whiteText.text = "White (P1) Pieces: " + whiteCount.ToString ();
displayDiff.text = "Difficulty: " + difficulty;
/*
if (IsGameOver()) {
if (blackCount > whiteCount)
gameoverText.text = "We lost this round, but it's not over yet.";
else if (whiteCount > blackCount)
gameoverText.text = "Never a doubt. Good job.";
else
gameoverText.text = "Stand back. It's a draw.";
restartText.text = "Press R to restart";
exitText.text = "Press Q to exit";
}
*/
}
// check whether the game is over
void CheckForWin()
{
if (AI.gameController.IsGameOver()) {
//blackCount = AI.gameController.CountPieces (AI.gameController.boardState, 2);
//whiteCount = AI.gameController.CountPieces (AI.gameController.boardState, 1);
if (blackCount > whiteCount) {
Debug.Log ("Black Player (AI) Wins!");
gameoverText.text = "We lost this round, but it's not over yet.";
gameoverText.color = Color.red;
if (!soundPlayed) {
bgmAudios[bgmIndex].Stop ();
loseAudio.Play ();
soundPlayed = true;
}
} else if (whiteCount > blackCount) {
Debug.Log ("White Player (P1) Wins!");
gameoverText.text = "Never a doubt. Good job.";
gameoverText.color = Color.blue;
if (!soundPlayed) {
bgmAudios[bgmIndex].Stop ();
winAudio.Play ();
soundPlayed = true;
}
} else {
Debug.Log ("Draw!");
gameoverText.text = "Stand back. It's a draw.";
gameoverText.color = Color.yellow;
}
restartText.text = "Press R to restart";
exitText.text = "Press Q to exit";
returnText.text = "Press T to return to Main Menu";
if (Input.GetKey (KeyCode.R)) {
//Application.LoadLevel (Application.loadedLevel);
SceneManager.LoadScene (SceneManager.GetActiveScene().name);
}
if (Input.GetKey (KeyCode.Q)) {
Application.Quit ();
}
if (Input.GetKey (KeyCode.T)) {
SceneManager.LoadScene ("MainMenu");
}
}
}
// flip the piece at position (x, z) on board
void FlipPiece(int[,] board, int x, int z)
{
if (board [x, z] == 1) {
// display the flip white to black animation
// so far we will just use substitution method
//boardPieces[x, z].transform.rotation *= Quaternion.Euler (new Vector3 (0f, 0f, -180f));
boardPieces [x, z].transform.Rotate (new Vector3 (0.0f, 0.0f, -180f));
board [x, z] = 2;
whiteCount--;
blackCount++;
UpdateText ();
} else if (board [x, z] == 2) {
// display the flip black to white animation
// so far we will just use substitution method
//boardPieces[x, z].transform.rotation *= Quaternion.Euler (new Vector3 (0f, 0f, 180f));
boardPieces [x, z].transform.Rotate (new Vector3 (0.0f, 0.0f, 180f));
board [x, z] = 1;
blackCount--;
whiteCount++;
UpdateText ();
} else
return;
}
// flip qualified pieces for the line that is defined by starting point (x, z) and direction dir
void FlipLine(int[,] board, int x, int z, Vector2 dir)
{
int changeX = (int)dir.x;
int changeZ = (int)dir.y;
int posX = x + changeX;
int posZ = z + changeZ;
while (board [posX, posZ] != AI.gameController.colorTurn) {
FlipPiece (board, posX, posZ);
posX += changeX;
posZ += changeZ;
}
}
// will be used to reduce the length of SetUpPiece()
public void MakeMove(int[,] board, int movex, int movez)
{
List<Vector2> flipDir;
GameObject clone;
// for human player
if (AI.gameController.colorTurn == 1) {
if (AI.gameController.IsValidMove (board, 1, movex, movez)) {
flipDir = AI.gameController.FindDir (board, 1, movex, movez);
for (int i = 0; i < flipDir.Count; i++) {
FlipLine (board, movex, movez, flipDir.ElementAt (i));
}
clone = Instantiate (piece, new Vector3 (movex, 1f, movez), Quaternion.identity) as GameObject;
boardPieces [movex, movez] = clone;
whiteCount++;
emptyLeft--;
UpdateText ();
board [movex, movez] = 1;
AI.gameController.colorTurn = 2;
//movesList = AI.gameController.MoveList (AI.gameController.boardState, 2);
//movesListPlayer = AI.gameController.MoveList (AI.gameController.boardState, 1);
movesList = AI.gameController.MoveList (board, 2);
movesListPlayer = AI.gameController.MoveList (board, 1);
Debug.Log ("After player moves, Black moves: " + movesList.Count);
Debug.Log ("After player moves, White moves: " + movesListPlayer.Count);
UpdateTurn ();
}
// for AI
} else if (AI.gameController.colorTurn == 2) {
if (AI.gameController.IsValidMove (board, 2, movex, movez)) {
flipDir = AI.gameController.FindDir (board, 2, movex, movez);
for (int i = 0; i < flipDir.Count; i++) {
FlipLine (board, movex, movez, flipDir.ElementAt (i));
}
clone = Instantiate (piece, new Vector3 (movex, 1f, movez), Quaternion.Euler (0.0f, 0.0f, 180f)) as GameObject;
boardPieces [movex, movez] = clone;
blackCount++;
emptyLeft--;
UpdateText ();
board [movex, movez] = 2;
AI.gameController.colorTurn = 1;
//movesList = AI.gameController.MoveList (AI.gameController.boardState, 1);
//movesListAI = AI.gameController.MoveList (AI.gameController.boardState, 2);
movesList = AI.gameController.MoveList (board, 1);
movesListAI = AI.gameController.MoveList (board, 2);
Debug.Log ("After AI moves, Black moves: " + movesListAI.Count);
Debug.Log ("After AI moves, White moves: " + movesList.Count);
UpdateTurn ();
}
}
}
// check whether game is over
public bool IsGameOver()
{
int playerCount = movesListPlayer.Count;
int AICount = movesListAI.Count;
if (playerCount == 0 && AICount == 0)
return true;
else
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment