引言
使用Java开发单机版五子棋游戏。当游戏的一方构成5个连续的棋子,无论是水平方向,垂直方向,还是斜对角线方向,都表示获胜了。并且支持悔棋和重开的功能。
游戏规则
先定义Chess类,得到棋盘中棋子的索引值和颜色,为判断五子连珠做准备。
package game06;
import java.awt.Color;
public class Chess {
private int x; //棋盘中x的索引值
private int y;
private Color color;//棋子颜色
static final int DIAMETER=30;
public Chess(int x, int y, Color color) {
super();
this.x = x;
this.y = y;
this.color = color;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
}
限定棋子所能下的位置,判断棋子是否可用。
//1.游戏结束不能下棋
if(gameOver){
return;
}
//2.落在棋盘外面,不能下
if(x_index<0||x_index>COLS||y_index>ROWS){
return;
}
//3.位置上已有棋子存在,不能下
if(findChess(x_index,y_index)){
return;
}
记录黑白方走棋,实现回合交换
Chess ch=new Chess(x_index,y_index,isBlack?Color.black:Color.white);
chessList[chessCount++]=ch;
System.out.print("棋子子个数: "+chessCount);
this.repaint();
判断赢的方法,四个方向(从西南到东北,从西北到东南,水平方向,垂直方向)只要实现五子连珠,即赢。
//位置上是否有棋子
private boolean findChess(int x,int y){
for(Chess c:chessList){
if(c!=null&&c.getX()==x&&c.getY()==y){
return true;
}
}
return false;
}
//得到棋盘上的棋子
private Chess getChess(int x,int y,Color color){
for(Chess c:chessList){
if(c!=null&&c.getX()==x&&c.getY()==y&&c.getColor()==color){
return c;
}
}
return null;
}
// 赢棋的方法,需要四个方向
private boolean isWin(){
return search1()||search2()||search3()||search4();
}
//1.从西南到东北方向
private boolean search1(){
int continueCount=1; //连续棋子的个数
//斜向上寻找
for(int x=x_index+1,y=y_index-1;x<=COLS&&y>=0;x++,y--){
Color c=isBlack?Color.black:Color.white;
if(getChess(x,y,c)!=null){
continueCount++;
}else{
break;
}
}
//斜向下寻找
for(int x=x_index-1,y=y_index+1;x<=ROWS&&x>=0;x--,y++){
Color c=isBlack?Color.black:Color.white;
if(getChess(x,y,c)!=null){
continueCount++;
}else{
break;
}
}
//五子连珠
if(continueCount>=5){
return true;
}else{
continueCount=1;
}
return false;
}
//2.水平左右-西东
private boolean search2(){
int continueCount=1; //连续棋子的个数
//寻找-西方向
for(int x=x_index-1;x>=0;x--){
Color c=isBlack?Color.black:Color.white;
if(getChess(x,y_index,c)!=null){
continueCount++;
}else{
break;
}
}
//寻找-东方向
for(int x=x_index+1;x<=COLS;x++){
Color c=isBlack?Color.black:Color.white;
if(getChess(x,y_index,c)!=null){
continueCount++;
}else{
break;
}
}
//五子连珠
if(continueCount>=5){
return true;
}else{
continueCount=1;
}
return false;
}
//西北到东南
private boolean search3(){
int continueCount=1; //连续棋子的个数
//斜向上寻找
for(int x=x_index-1,y=y_index-1;x>=0&&y>=0;x--,y--){
Color c=isBlack?Color.black:Color.white;
if(getChess(x,y,c)!=null){
continueCount++;
}else{
break;
}
}
//斜向下寻找
for(int x=x_index+1,y=y_index+1;x<=COLS&&y<ROWS;x++,y++){
Color c=isBlack?Color.black:Color.white;
if(getChess(x,y,c)!=null){
continueCount++;
}else{
break;
}
}
//五子连珠
if(continueCount>=5){
return true;
}else{
continueCount=1;
}
return false;
}
//垂直向下
private boolean search4(){
int continueCount=1; //连续棋子的个数
//寻找-向上的方向
for(int y=y_index-1;y>=0;y--){
Color c=isBlack?Color.black:Color.white;
if(getChess(x_index,y,c)!=null){
continueCount++;
}else{
break;
}
}
//寻找-向下的方向
for(int y=y_index+1;y<=ROWS;y++){
Color c=isBlack?Color.black:Color.white;
if(getChess(x_index,y,c)!=null){
continueCount++;
}else{
break;
}
}
//五子连珠
if(continueCount>=5){
return true;
}else{
continueCount=1;
}
return false;
}
游戏功能:重开
public void restartGame(){
//清除棋子
for(int i=0;i<chessList.length;i++){
chessList[i]=null;
}
//恢复游戏相关的变量
isBlack=true;
gameOver=false;
chessCount=0;
this.repaint();
}
游戏功能:悔棋
void goback(){
//棋盘中没有棋子,不能悔棋
if(chessCount==0){
return;
}
chessList[chessCount-1]=null;
chessCount--;
if(chessCount>0){
x_index=chessList[chessCount-1].getX();
y_index=chessList[chessCount-1].getY();
}
isBlack=!isBlack;
this.repaint();
}