Unity3D 2019.3开发的中国象棋的源代码
1 ChessEnum
public enum ChessSide
{
RED,
BLACK,
Barrier
}
public enum ChessType
{
Soldier= 0 ,
Cannon= 1 ,
Chariot= 2 ,
Elephant = 3 ,
Horse= 4 ,
Advisor= 5 ,
General = 6 ,
Barrier = 7
}
public class ChessKey {
public ChessSide side;
public ChessType type;
public ChessKey ( ) { }
public ChessKey ( ChessSide side, ChessType type) {
this . side = side;
this . type = type;
}
}
2 ChessManger
using System. Collections;
using System. Collections. Generic;
using UnityEngine;
using UnityEngine. SceneManagement;
public class ChessManger : MonoBehaviour
{
public static ChessManger instance;
public GameObject chessPrefab;
public int xColumn = 9 ;
public int yRow = 10 ;
public int [ , ] chessArray = new int [ , ]
{
{ 2 , - 1 , - 1 , 0 , - 1 , - 1 , 0 , - 1 , - 1 , 2 } ,
{ 4 , - 1 , 1 , - 1 , - 1 , - 1 , - 1 , 1 , - 1 , 4 } ,
{ 3 , - 1 , - 1 , 0 , - 1 , - 1 , 0 , - 1 , - 1 , 3 } ,
{ 5 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , 5 } ,
{ 6 , - 1 , - 1 , 0 , - 1 , - 1 , 0 , - 1 , - 1 , 6 } ,
{ 5 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , 5 } ,
{ 3 , - 1 , - 1 , 0 , - 1 , - 1 , 0 , - 1 , - 1 , 3 } ,
{ 4 , - 1 , 1 , - 1 , - 1 , - 1 , - 1 , 1 , - 1 , 4 } ,
{ 2 , - 1 , - 1 , 0 , - 1 , - 1 , 0 , - 1 , - 1 , 2 } ,
} ;
public GameObject[ , ] gameObjectsArray;
public LinkedList< GameObject> BarrierGameObject = new LinkedList < GameObject> ( ) ;
public Piece selectPiece = null;
public Piece lastSelectPiece = null;
private void Awake ( )
{
gameObjectsArray = new GameObject [ xColumn, yRow] ;
for ( int x = 0 ; x < xColumn; x++ )
{
for ( int y = 0 ; y < yRow; y++ )
{
ChessSide tempside = new ChessSide ( ) ;
if ( chessArray[ x, y] != - 1 )
{
if ( y <= 5 ) { tempside = ChessSide. RED; } else { tempside = ChessSide. BLACK; }
ChessType tempType = ( ChessType) chessArray[ x, y] ;
GameObject tempGameObject = GameObject. Instantiate ( chessPrefab, correctPosition ( x, y, 1 ) , Quaternion. identity) ;
Piece tempPiece = tempGameObject. GetComponent< Piece> ( ) ;
tempGameObject. transform. parent = transform;
ChessKey tempChessKey = new ChessKey ( ) ;
tempChessKey. side = tempside;
tempChessKey. type = tempType;
tempPiece. init ( x, y, tempChessKey) ;
gameObjectsArray[ x, y] = tempGameObject;
}
}
}
instance = this ;
}
public ChessSide currentChessSide = ChessSide. BLACK;
void Start ( )
{
currentChessSide = SwitchSide. instance. chessSideMethon ( currentChessSide) ;
}
Vector3 correctPosition ( float x, float y, float z) {
float tempX;
float tempY;
tempX = ( x - xColumn / 2 ) ;
tempY = ( y - ( yRow) / 2 ) ;
return new Vector3 ( tempX, tempY, z) ;
}
public List< Vector2> getMoveable ( Piece piece) {
int x0 = 3 ;
int x1 = 5 ;
int y2 = - 1 ;
int y3 = - 1 ;
List< Vector2> moveables = new List < Vector2> ( ) ;
switch ( piece. chessKey. type)
{
case ChessType. Soldier:
if ( piece. chessKey. side == ChessSide. RED)
{
if ( piece. y <= 4 )
{
if ( gameObjectsArray[ piece. x, piece. y + 1 ] != null &&
gameObjectsArray[ piece. x, piece. y + 1 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x, piece. y + 1 ) ) ;
}
else if ( gameObjectsArray[ piece. x, piece. y + 1 ] == null)
{
moveables. Add ( new Vector2 ( piece. x, piece. y + 1 ) ) ;
}
} else if ( piece. y < yRow) {
if ( piece. y + 1 < yRow) {
if ( gameObjectsArray[ piece. x, piece. y + 1 ] != null &&
gameObjectsArray[ piece. x, piece. y + 1 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x, piece. y + 1 ) ) ;
}
else if ( gameObjectsArray[ piece. x, piece. y + 1 ] == null)
{
moveables. Add ( new Vector2 ( piece. x, piece. y + 1 ) ) ;
}
}
if ( piece. x - 1 >= 0 )
{
if ( gameObjectsArray[ piece. x - 1 , piece. y] != null &&
gameObjectsArray[ piece. x - 1 , piece. y] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x - 1 , piece. y) ) ;
}
else if ( gameObjectsArray[ piece. x - 1 , piece. y] == null)
{
moveables. Add ( new Vector2 ( piece. x - 1 , piece. y) ) ;
}
}
if ( piece. x + 1 < xColumn)
{
if ( gameObjectsArray[ piece. x + 1 , piece. y] != null &&
gameObjectsArray[ piece. x + 1 , piece. y] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x + 1 , piece. y) ) ;
}
else if ( gameObjectsArray[ piece. x + 1 , piece. y] == null)
{
moveables. Add ( new Vector2 ( piece. x + 1 , piece. y) ) ;
}
}
}
}
else if ( piece. chessKey. side == ChessSide. BLACK)
{
if ( piece. y >= 5 )
{
if ( gameObjectsArray[ piece. x, piece. y - 1 ] != null &&
gameObjectsArray[ piece. x, piece. y - 1 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x, piece. y - 1 ) ) ;
}
else if ( gameObjectsArray[ piece. x, piece. y - 1 ] == null)
{
moveables. Add ( new Vector2 ( piece. x, piece. y - 1 ) ) ;
}
}
else if ( piece. y >= 0 )
{
if ( piece. y - 1 >= 0 )
{
if ( gameObjectsArray[ piece. x, piece. y - 1 ] != null &&
gameObjectsArray[ piece. x, piece. y - 1 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x, piece. y - 1 ) ) ;
}
else if ( gameObjectsArray[ piece. x, piece. y - 1 ] == null)
{
moveables. Add ( new Vector2 ( piece. x, piece. y - 1 ) ) ;
}
}
if ( piece. x - 1 >= 0 )
{
if ( gameObjectsArray[ piece. x - 1 , piece. y] != null &&
gameObjectsArray[ piece. x - 1 , piece. y] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x - 1 , piece. y) ) ;
}
else if ( gameObjectsArray[ piece. x - 1 , piece. y] == null)
{
moveables. Add ( new Vector2 ( piece. x - 1 , piece. y) ) ;
}
}
if ( piece. x + 1 < xColumn)
{
if ( gameObjectsArray[ piece. x + 1 , piece. y] != null &&
gameObjectsArray[ piece. x + 1 , piece. y] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x + 1 , piece. y) ) ;
}
else if ( gameObjectsArray[ piece. x + 1 , piece. y] == null)
{
moveables. Add ( new Vector2 ( piece. x + 1 , piece. y) ) ;
}
}
}
}
break ;
case ChessType. Cannon:
for ( int x = piece. x - 1 ; x >= 0 ; x-- )
{
if ( gameObjectsArray[ x, piece. y] != null)
{
break ;
}
moveables. Add ( new Vector2 ( x, piece. y) ) ;
}
for ( int x = piece. x + 1 ; x < xColumn; x++ )
{
if ( gameObjectsArray[ x, piece. y] != null)
{
break ;
}
moveables. Add ( new Vector2 ( x, piece. y) ) ;
}
for ( int y = piece. y + 1 ; y < yRow; y++ )
{
if ( gameObjectsArray[ piece. x, y] != null)
{
break ;
}
moveables. Add ( new Vector2 ( piece. x, y) ) ;
}
for ( int y = piece. y - 1 ; y >= 0 ; y-- )
{
if ( gameObjectsArray[ piece. x, y] != null)
{
break ;
}
moveables. Add ( new Vector2 ( piece. x, y) ) ;
}
for ( int x = piece. x - 1 ; x >= 0 ; x-- )
{
if ( gameObjectsArray[ x, piece. y] != null)
{
for ( x = x - 1 ; x >= 0 ; x-- )
{
if ( gameObjectsArray[ x, piece. y] != null)
{
Piece tempPiece = gameObjectsArray[ x, piece. y] . GetComponent< Piece> ( ) ;
if ( piece. chessKey. side != tempPiece. chessKey. side)
{
moveables. Add ( new Vector2 ( x, piece. y) ) ;
}
break ;
}
}
break ;
}
}
for ( int x = piece. x + 1 ; x < xColumn; x++ )
{
if ( gameObjectsArray[ x, piece. y] != null)
{
for ( x = x + 1 ; x < xColumn; x++ )
{
if ( gameObjectsArray[ x, piece. y] != null)
{
Piece tempPiece = gameObjectsArray[ x, piece. y] . GetComponent< Piece> ( ) ;
if ( piece. chessKey. side != tempPiece. chessKey. side)
{
moveables. Add ( new Vector2 ( x, piece. y) ) ;
}
break ;
}
}
break ;
}
}
for ( int y = piece. y - 1 ; y >= 0 ; y-- )
{
if ( gameObjectsArray[ piece. x, y] != null)
{
for ( y = y - 1 ; y >= 0 ; y-- )
{
if ( gameObjectsArray[ piece. x, y] != null)
{
Piece tempPiece = gameObjectsArray[ piece. x, y] . GetComponent< Piece> ( ) ;
if ( piece. chessKey. side != tempPiece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x, y) ) ;
}
break ;
}
}
break ;
}
}
for ( int y = piece. y + 1 ; y < yRow; y++ )
{
if ( gameObjectsArray[ piece. x, y] != null)
{
for ( y = y + 1 ; y < yRow; y++ )
{
if ( gameObjectsArray[ piece. x, y] != null)
{
Piece tempPiece = gameObjectsArray[ piece. x, y] . GetComponent< Piece> ( ) ;
if ( piece. chessKey. side != tempPiece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x, y) ) ;
}
break ;
}
}
break ;
}
}
break ;
case ChessType. Chariot:
for ( int x = piece. x - 1 ; x >= 0 ; x-- )
{
if ( gameObjectsArray[ x, piece. y] != null)
{
break ;
}
moveables. Add ( new Vector2 ( x, piece. y) ) ;
}
for ( int x = piece. x + 1 ; x < xColumn; x++ )
{
if ( gameObjectsArray[ x, piece. y] != null)
{
break ;
}
moveables. Add ( new Vector2 ( x, piece. y) ) ;
}
for ( int y = piece. y + 1 ; y < yRow; y++ )
{
if ( gameObjectsArray[ piece. x, y] != null)
{
break ;
}
moveables. Add ( new Vector2 ( piece. x, y) ) ;
}
for ( int y = piece. y - 1 ; y >= 0 ; y-- )
{
if ( gameObjectsArray[ piece. x, y] != null)
{
break ;
}
moveables. Add ( new Vector2 ( piece. x, y) ) ;
}
for ( int x = piece. x - 1 ; x >= 0 ; x-- )
{
if ( gameObjectsArray[ x, piece. y] != null)
{
Piece tempPiece = gameObjectsArray[ x, piece. y] . GetComponent< Piece> ( ) ;
if ( piece. chessKey. side != tempPiece. chessKey. side)
{
moveables. Add ( new Vector2 ( x, piece. y) ) ;
}
break ;
}
}
for ( int x = piece. x + 1 ; x < xColumn; x++ )
{
if ( gameObjectsArray[ x, piece. y] != null)
{
Piece tempPiece = gameObjectsArray[ x, piece. y] . GetComponent< Piece> ( ) ;
if ( piece. chessKey. side != tempPiece. chessKey. side)
{
moveables. Add ( new Vector2 ( x, piece. y) ) ;
}
break ;
}
}
for ( int y = piece. y + 1 ; y < yRow; y++ )
{
if ( gameObjectsArray[ piece. x, y] != null)
{
Piece tempPiece = gameObjectsArray[ piece. x, y] . GetComponent< Piece> ( ) ;
if ( piece. chessKey. side != tempPiece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x, y) ) ;
}
break ;
}
}
for ( int y = piece. y - 1 ; y >= 0 ; y-- )
{
if ( gameObjectsArray[ piece. x, y] != null)
{
Piece tempPiece = gameObjectsArray[ piece. x, y] . GetComponent< Piece> ( ) ;
if ( piece. chessKey. side != tempPiece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x, y) ) ;
}
break ;
}
}
break ;
case ChessType. Elephant:
int y0 = - 1 ;
int y1 = - 1 ;
if ( piece. chessKey. side == ChessSide. RED)
{
y0 = 0 ;
y1 = 4 ;
}
else if ( piece. chessKey. side == ChessSide. BLACK)
{
y0 = 5 ;
y1 = 9 ;
}
if ( piece. x - 2 >= 0 && piece. y + 2 <= y1)
{
if ( gameObjectsArray[ piece. x - 1 , piece. y + 1 ] == null)
{
if ( gameObjectsArray[ piece. x - 2 , piece. y + 2 ] != null &&
gameObjectsArray[ piece. x - 2 , piece. y + 2 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x - 2 , piece. y + 2 ) ) ;
}
else if ( gameObjectsArray[ piece. x - 2 , piece. y + 2 ] == null)
{
moveables. Add ( new Vector2 ( piece. x - 2 , piece. y + 2 ) ) ;
}
}
}
if ( piece. x - 2 >= 0 && piece. y - 2 >= y0)
{
if ( gameObjectsArray[ piece. x - 1 , piece. y - 1 ] == null)
{
if ( gameObjectsArray[ piece. x - 2 , piece. y - 2 ] != null &&
gameObjectsArray[ piece. x - 2 , piece. y - 2 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x - 2 , piece. y - 2 ) ) ;
}
else if ( gameObjectsArray[ piece. x - 2 , piece. y - 2 ] == null)
{
moveables. Add ( new Vector2 ( piece. x - 2 , piece. y - 2 ) ) ;
}
}
}
if ( piece. x + 2 < xColumn && piece. y + 2 <= y1)
{
if ( gameObjectsArray[ piece. x + 1 , piece. y + 1 ] == null)
{
if ( gameObjectsArray[ piece. x + 2 , piece. y + 2 ] != null &&
gameObjectsArray[ piece. x + 2 , piece. y + 2 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x + 2 , piece. y + 2 ) ) ;
}
else if ( gameObjectsArray[ piece. x + 2 , piece. y + 2 ] == null)
{
moveables. Add ( new Vector2 ( piece. x + 2 , piece. y + 2 ) ) ;
}
}
}
if ( piece. x + 2 < xColumn && piece. y - 2 >= y0)
{
if ( gameObjectsArray[ piece. x + 1 , piece. y - 1 ] == null)
{
if ( gameObjectsArray[ piece. x + 2 , piece. y - 2 ] != null &&
gameObjectsArray[ piece. x + 2 , piece. y - 2 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x + 2 , piece. y - 2 ) ) ;
}
else if ( gameObjectsArray[ piece. x + 2 , piece. y - 2 ] == null)
{
moveables. Add ( new Vector2 ( piece. x + 2 , piece. y - 2 ) ) ;
}
}
}
break ;
case ChessType. Horse:
if ( piece. x - 1 >= 0 && piece. y + 2 < yRow)
{
if ( gameObjectsArray[ piece. x, piece. y + 1 ] == null)
{
if ( gameObjectsArray[ piece. x - 1 , piece. y + 2 ] != null &&
gameObjectsArray[ piece. x - 1 , piece. y + 2 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x - 1 , piece. y + 2 ) ) ;
}
else if ( gameObjectsArray[ piece. x - 1 , piece. y + 2 ] == null)
{
moveables. Add ( new Vector2 ( piece. x - 1 , piece. y + 2 ) ) ;
}
}
}
if ( piece. x - 2 >= 0 && piece. y + 1 < yRow)
{
if ( gameObjectsArray[ piece. x - 1 , piece. y] == null)
{
if ( gameObjectsArray[ piece. x - 2 , piece. y + 1 ] != null &&
gameObjectsArray[ piece. x - 2 , piece. y + 1 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x - 2 , piece. y + 1 ) ) ;
}
else if ( gameObjectsArray[ piece. x - 2 , piece. y + 1 ] == null)
{
moveables. Add ( new Vector2 ( piece. x - 2 , piece. y + 1 ) ) ;
}
}
}
if ( piece. x + 1 < xColumn && piece. y + 2 < yRow)
{
if ( gameObjectsArray[ piece. x, piece. y + 1 ] == null)
{
if ( gameObjectsArray[ piece. x + 1 , piece. y + 2 ] != null &&
gameObjectsArray[ piece. x + 1 , piece. y + 2 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x + 1 , piece. y + 2 ) ) ;
}
else if ( gameObjectsArray[ piece. x + 1 , piece. y + 2 ] == null)
{
moveables. Add ( new Vector2 ( piece. x + 1 , piece. y + 2 ) ) ;
}
}
}
if ( piece. x + 2 < xColumn && piece. y + 1 < yRow)
{
if ( gameObjectsArray[ piece. x + 1 , piece. y] == null)
{
if ( gameObjectsArray[ piece. x + 2 , piece. y + 1 ] != null &&
gameObjectsArray[ piece. x + 2 , piece. y + 1 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x + 2 , piece. y + 1 ) ) ;
}
else if ( gameObjectsArray[ piece. x + 2 , piece. y + 1 ] == null)
{
moveables. Add ( new Vector2 ( piece. x + 2 , piece. y + 1 ) ) ;
}
}
}
if ( piece. x - 1 >= 0 && piece. y - 2 >= 0 )
{
if ( gameObjectsArray[ piece. x, piece. y - 1 ] == null)
{
if ( gameObjectsArray[ piece. x - 1 , piece. y - 2 ] != null &&
gameObjectsArray[ piece. x - 1 , piece. y - 2 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x - 1 , piece. y - 2 ) ) ;
}
else if ( gameObjectsArray[ piece. x - 1 , piece. y - 2 ] == null)
{
moveables. Add ( new Vector2 ( piece. x - 1 , piece. y - 2 ) ) ;
}
}
}
if ( piece. x - 2 >= 0 && piece. y - 1 >= 0 )
{
if ( gameObjectsArray[ piece. x - 1 , piece. y] == null)
{
if ( gameObjectsArray[ piece. x - 2 , piece. y - 1 ] != null &&
gameObjectsArray[ piece. x - 2 , piece. y - 1 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x - 2 , piece. y - 1 ) ) ;
}
else if ( gameObjectsArray[ piece. x - 2 , piece. y - 1 ] == null)
{
moveables. Add ( new Vector2 ( piece. x - 2 , piece. y - 1 ) ) ;
}
}
}
if ( piece. x + 1 < xColumn && piece. y - 2 >= 0 )
{
if ( gameObjectsArray[ piece. x, piece. y - 1 ] == null)
{
if ( gameObjectsArray[ piece. x + 1 , piece. y - 2 ] != null &&
gameObjectsArray[ piece. x + 1 , piece. y - 2 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x + 1 , piece. y - 2 ) ) ;
}
else if ( gameObjectsArray[ piece. x + 1 , piece. y - 2 ] == null)
{
moveables. Add ( new Vector2 ( piece. x + 1 , piece. y - 2 ) ) ;
}
}
}
if ( piece. x + 2 < xColumn && piece. y - 1 >= 0 )
{
if ( gameObjectsArray[ piece. x + 1 , piece. y] == null)
{
if ( gameObjectsArray[ piece. x + 2 , piece. y - 1 ] != null &&
gameObjectsArray[ piece. x + 2 , piece. y - 1 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x + 2 , piece. y - 1 ) ) ;
}
else if ( gameObjectsArray[ piece. x + 2 , piece. y - 1 ] == null)
{
moveables. Add ( new Vector2 ( piece. x + 2 , piece. y - 1 ) ) ;
}
}
}
break ;
case ChessType. General:
if ( piece. chessKey. side == ChessSide. RED)
{
y2 = 0 ;
y3 = 2 ;
for ( int y = piece. y + 1 ; y < yRow; y++ )
{
if ( gameObjectsArray[ piece. x, y] != null &&
gameObjectsArray[ piece. x, y] . GetComponent< Piece> ( ) . chessKey. type == ChessType. General)
{
moveables. Add ( new Vector2 ( piece. x, y) ) ;
}
else if ( gameObjectsArray[ piece. x, y] != null &&
gameObjectsArray[ piece. x, y] . GetComponent< Piece> ( ) . chessKey. type != ChessType. General)
{
break ;
}
else if ( gameObjectsArray[ piece. x, y] == null)
{
continue ;
}
}
}
else if ( piece. chessKey. side == ChessSide. BLACK)
{
y2 = 7 ;
y3 = 9 ;
for ( int y = piece. y - 1 ; y >= 0 ; y-- )
{
if ( gameObjectsArray[ piece. x, y] != null &&
gameObjectsArray[ piece. x, y] . GetComponent< Piece> ( ) . chessKey. type == ChessType. General)
{
moveables. Add ( new Vector2 ( piece. x, y) ) ;
}
else if ( gameObjectsArray[ piece. x, y] != null &&
gameObjectsArray[ piece. x, y] . GetComponent< Piece> ( ) . chessKey. type != ChessType. General)
{
break ;
}
else if ( gameObjectsArray[ piece. x, y] == null)
{
continue ;
}
}
}
if ( piece. x - 1 >= x0)
{
if ( gameObjectsArray[ piece. x - 1 , piece. y] != null &&
gameObjectsArray[ piece. x - 1 , piece. y] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x - 1 , piece. y) ) ;
}
else if ( gameObjectsArray[ piece. x - 1 , piece. y] == null)
{
moveables. Add ( new Vector2 ( piece. x - 1 , piece. y) ) ;
}
}
if ( piece. y - 1 >= y2)
{
if ( gameObjectsArray[ piece. x, piece. y - 1 ] != null &&
gameObjectsArray[ piece. x, piece. y - 1 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x, piece. y - 1 ) ) ;
}
else if ( gameObjectsArray[ piece. x, piece. y - 1 ] == null)
{
moveables. Add ( new Vector2 ( piece. x, piece. y - 1 ) ) ;
}
}
if ( piece. x + 1 <= x1)
{
if ( gameObjectsArray[ piece. x + 1 , piece. y] != null &&
gameObjectsArray[ piece. x + 1 , piece. y] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x + 1 , piece. y) ) ;
}
else if ( gameObjectsArray[ piece. x + 1 , piece. y] == null)
{
moveables. Add ( new Vector2 ( piece. x + 1 , piece. y) ) ;
}
}
if ( piece. y + 1 <= y3)
{
if ( gameObjectsArray[ piece. x, piece. y + 1 ] != null &&
gameObjectsArray[ piece. x, piece. y + 1 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x, piece. y + 1 ) ) ;
}
else if ( gameObjectsArray[ piece. x, piece. y + 1 ] == null)
{
moveables. Add ( new Vector2 ( piece. x, piece. y + 1 ) ) ;
}
}
break ;
case ChessType. Advisor:
if ( piece. chessKey. side == ChessSide. RED)
{
y2 = 0 ;
y3 = 2 ;
}
else if ( piece. chessKey. side == ChessSide. BLACK)
{
y2 = 7 ;
y3 = 9 ;
}
if ( piece. x - 1 >= x0 && piece. y + 1 <= y3)
{
if ( gameObjectsArray[ piece. x - 1 , piece. y + 1 ] != null &&
gameObjectsArray[ piece. x - 1 , piece. y + 1 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x - 1 , piece. y + 1 ) ) ;
}
else if ( gameObjectsArray[ piece. x - 1 , piece. y + 1 ] == null)
{
moveables. Add ( new Vector2 ( piece. x - 1 , piece. y + 1 ) ) ;
}
}
if ( piece. x - 1 >= x0 && piece. y - 1 >= y2)
{
if ( gameObjectsArray[ piece. x - 1 , piece. y - 1 ] != null &&
gameObjectsArray[ piece. x - 1 , piece. y - 1 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x - 1 , piece. y - 1 ) ) ;
}
else if ( gameObjectsArray[ piece. x - 1 , piece. y - 1 ] == null)
{
moveables. Add ( new Vector2 ( piece. x - 1 , piece. y - 1 ) ) ;
}
}
if ( piece. x + 1 <= x1 && piece. y + 1 <= y3)
{
if ( gameObjectsArray[ piece. x + 1 , piece. y + 1 ] != null &&
gameObjectsArray[ piece. x + 1 , piece. y + 1 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x + 1 , piece. y + 1 ) ) ;
}
else if ( gameObjectsArray[ piece. x + 1 , piece. y + 1 ] == null)
{
moveables. Add ( new Vector2 ( piece. x + 1 , piece. y + 1 ) ) ;
}
}
if ( piece. x + 1 <= x1 && piece. y - 1 >= y2)
{
if ( gameObjectsArray[ piece. x + 1 , piece. y - 1 ] != null &&
gameObjectsArray[ piece. x + 1 , piece. y - 1 ] . GetComponent< Piece> ( ) . chessKey. side != piece. chessKey. side)
{
moveables. Add ( new Vector2 ( piece. x + 1 , piece. y - 1 ) ) ;
}
else if ( gameObjectsArray[ piece. x + 1 , piece. y - 1 ] == null)
{
moveables. Add ( new Vector2 ( piece. x + 1 , piece. y - 1 ) ) ;
}
}
break ;
case ChessType. Barrier:
break ;
default :
break ;
}
return moveables;
}
public void onMousePressEvents ( Piece piece) {
if ( selectPiece != null)
{
return ;
}
selectPiece = piece;
if ( selectPiece. chessKey. side != currentChessSide)
{
selectPiece = null;
return ;
}
List< Vector2> moveablepiece = getMoveable ( piece) ;
if ( moveablepiece. Count <= 0 )
{
selectPiece = null;
return ;
}
foreach ( Vector2 item in moveablepiece)
{
GameObject tempGameObject = Instantiate ( chessPrefab, correctPosition ( item. x, item. y, - 3 ) , Quaternion. identity) ;
Piece tempPiece = tempGameObject. GetComponent< Piece> ( ) ;
ChessKey tempChessKey = new ChessKey ( ) ;
tempChessKey. side = ChessSide. Barrier;
tempChessKey. type = ChessType. Barrier;
tempPiece. init ( ( int ) ( item. x) , ( int ) item. y, tempChessKey) ;
BarrierGameObject. AddFirst ( tempGameObject) ;
}
}
public void onMouseUpEvents ( ) {
if ( lastSelectPiece == null)
{
return ;
}
chessArray[ selectPiece. x, selectPiece. y] = - 1 ;
gameObjectsArray[ selectPiece. x, selectPiece. y] = null;
chessArray[ lastSelectPiece. x, lastSelectPiece. y] = ( int ) selectPiece. chessKey. type;
selectPiece. x = lastSelectPiece. x;
selectPiece. y = lastSelectPiece. y;
selectPiece. transform. position = lastSelectPiece. transform. position;
if ( gameObjectsArray[ lastSelectPiece. x, lastSelectPiece. y] != null)
{
DestroyImmediate ( gameObjectsArray[ lastSelectPiece. x, lastSelectPiece. y] ) ;
}
gameObjectsArray[ lastSelectPiece. x, lastSelectPiece. y] = selectPiece. transform. gameObject;
foreach ( GameObject item in BarrierGameObject)
{
DestroyImmediate ( item) ;
}
currentChessSide = SwitchSide. instance. chessSideMethon ( currentChessSide) ;
selectPiece = null;
lastSelectPiece = null;
}
public void onMouseOverEvents ( Piece piece)
{
if ( piece. chessKey. type == ChessType. Barrier)
{
lastSelectPiece = piece;
selectPiece. transform. position = piece. transform. position;
}
}
void Update ( )
{
if ( selectPiece != null && lastSelectPiece == null & Input. GetMouseButtonDown ( 1 ) )
{
selectPiece = null;
foreach ( GameObject item in BarrierGameObject)
{
DestroyImmediate ( item) ;
}
}
}
public void restart ( ) {
SceneManager. LoadScene ( SceneManager. GetActiveScene ( ) . buildIndex) ;
}
}
3 ChessMove
using System. Collections;
using System. Collections. Generic;
using UnityEngine;
public class ChessMove : MonoBehaviour
{
public Piece piece;
private void Awake ( )
{
piece = this . transform. GetComponent< Piece> ( ) ;
}
private void OnMouseDown ( )
{
ChessManger. instance. onMousePressEvents ( this . piece) ;
}
private void OnMouseUp ( )
{
ChessManger. instance. onMouseUpEvents ( ) ;
}
private void OnMouseEnter ( )
{
ChessManger. instance. onMouseOverEvents ( this . piece) ;
}
}
4 Piece
using System. Collections;
using System. Collections. Generic;
using UnityEngine;
public class Piece : MonoBehaviour
{
public int x;
public int y;
public ChessKey chessKey;
SpriteRenderer spriteRenderer;
private void Awake ( )
{
spriteRenderer = this . transform. GetComponent< SpriteRenderer> ( ) ;
}
public void init ( int x, int y, ChessKey chessKey) {
this . x = x;
this . y = y;
this . chessKey = chessKey;
spriteRenderer. sprite = PieceDirect. getSprite ( chessKey) ;
}
public override string ToString ( )
{
return "x:" + x + "y:" + y + "chessSide:" + chessKey. side + "chessType:" + chessKey. type;
}
}
5 PieceDirect
using System. Collections;
using System. Collections. Generic;
using UnityEngine;
public class PieceDirect : MonoBehaviour
{
[ System. Serializable]
public struct ChessSideStruct
{
public ChessSide side;
public Sprite sprite;
}
[ System. Serializable]
public struct ChessTypeStruct
{
public ChessType type;
public ChessSideStruct sideStruct;
}
public ChessTypeStruct[ ] chessStructs;
public static Dictionary< ChessKey, Sprite> chessDirct;
void Awake ( )
{
chessDirct = new Dictionary < ChessKey, Sprite> ( ) ;
for ( int i = 0 ; i < chessStructs. Length; i++ )
{
ChessType chessType = chessStructs[ i] . type;
ChessSideStruct chessSideStruct = chessStructs[ i] . sideStruct;
ChessSide chessSide = chessSideStruct. side;
Sprite sprite = chessSideStruct. sprite;
ChessKey tempchessKey = new ChessKey ( ) ;
tempchessKey. side = chessSide;
tempchessKey. type = chessType;
chessDirct. Add ( tempchessKey, sprite) ;
}
}
public static Sprite getSprite ( ChessKey chessKey)
{
Sprite tempSprite = null;
foreach ( ChessKey temp in chessDirct. Keys)
{
if ( temp. side == chessKey. side && temp. type == chessKey. type) {
tempSprite= chessDirct[ temp] ;
break ;
}
}
return tempSprite;
}
}
6 SwitchSide
using System. Collections;
using System. Collections. Generic;
using UnityEngine;
using UnityEngine. UI;
public class SwitchSide : MonoBehaviour
{
public static SwitchSide instance;
public Text sideDes;
public Image sideSprite;
public ChessKey tempKey;
void Awake ( )
{
instance = this ;
}
public ChessSide chessSideMethon ( ChessSide chessSide) {
tempKey = new ChessKey ( ) ;
tempKey. type = ChessType. General;
switch ( chessSide)
{
case ChessSide. RED:
tempKey. side = ChessSide. BLACK;
Debug. Log ( tempKey. side) ;
break ;
case ChessSide. BLACK:
tempKey. side = ChessSide. RED;
Debug. Log ( tempKey. side) ;
break ;
case ChessSide. Barrier:
break ;
default :
break ;
}
Debug. Log ( tempKey. side) ;
return tempKey. side;
}
void Update ( )
{
if ( tempKey== null)
{
return ;
}
switch ( tempKey. side)
{
case ChessSide. BLACK:
sideDes. text = "黑方" . ToString ( ) ;
sideSprite. sprite = PieceDirect. getSprite ( tempKey) ;
Debug. Log ( tempKey. side) ;
break ;
case ChessSide. RED:
sideDes. text = "红方" . ToString ( ) ;
sideSprite. sprite = PieceDirect. getSprite ( tempKey) ;
break ;
}
}
}