import java.util.Scanner
public class Games {
private String board[][]
private static int SIZE = 17
private static String roles = "A玩家"
//初始化数组
public void initBoard() {
board = new String[SIZE][SIZE]
for (int i = 0i <SIZEi++) {
for (int j = 0j <SIZEj++) {
// if(i==0){
//String str = ""
//str += j+" "
//board[i][j]= str
// }else if(i!=0&&j==0){
// String str = ""
// str += i+" "
//board[i][j]= str
// }else{
board[i][j] = "╋"
// }
}
}
}
//输出棋盘
public void printBoard() {
for (int i = 0i <SIZEi++) {
for (int j = 0j <SIZEj++) {
System.out.print(board[i][j])
}
System.out.println()
}
}
//判断所下棋子位置是否合理
public boolean isOk(int x, int y) {
boolean isRight = true
if (x >= 16 || x <1 || y >= 16 | y <1) {
//System.out.println("输入错误,请从新输入")
isRight = false
}
if (board[x][y].equals("●") || board[x][y].equals("○")) {
isRight = false
}
return isRight
}
//判断谁赢了
public void whoWin(Games wz) {
// 从数组挨个查找找到某个类型的棋子就从该棋子位置向右,向下,斜向右下 各查找5连续的位置看是否为5个相同的
int xlabel// 记录第一次找到某个棋子的x坐标
int ylabel// 记录第一次找到某个棋子的y坐标
// ●○╋
// 判断人是否赢了
for (int i = 0i <SIZEi++) {
for (int j = 0j <SIZEj++) {
if (board[i][j].equals("○")) {
xlabel = i
ylabel = j
// 横向找 x坐标不变 y坐标以此加1连成字符串
String heng = ""
if (i + 5 <SIZE &&j + 5 <SIZE) {
for (int k = jk <j + 5k++) {
heng += board[i][k]
}
if (heng.equals("○○○○○")) {
System.out.println(roles+"赢了!您输了!")
System.exit(0)
}
// 向下判断y不变 x逐增5 连成字符串
String xia = ""
for (int l = jl <i + 5l++) {
xia += board[l][j]
// System.out.println(xia)
}
if (xia.equals("○○○○○")) {
System.out.println(roles+"赢了!您输了!")
System.exit(0)
}
// 斜向右下判断
String youxia = ""
for (int a = 1a <= 5a++) {
youxia += board[xlabel++][ylabel++]
}
if (youxia.equals("○○○○○")) {
System.out.println(roles+"赢了!您输了!")
System.exit(0)
}
}
}
}
}
// 判断电脑是否赢了
for (int i = 0i <SIZEi++) {
for (int j = 0j <SIZEj++) {
if (board[i][j].equals("●")) {
xlabel = i
ylabel = j
// 横向找 x坐标不变 y坐标以此加1连成字符串
String heng = ""
if (j + 5 <SIZE &&i + 5 <SIZE) {
for (int k = jk <j + 5k++) {
heng += board[i][k]
}
if (heng.equals("●●●●●")) {
System.out.println(roles+"赢输了!您输了!")
System.exit(0)
}
// 向下判断y不变 x逐增5 连成字符串
String xia = ""
for (int l = il <i + 5l++) {
xia += board[l][ylabel]
// System.out.println(xia)
}
if (xia.equals("●●●●●")) {
System.out.println(roles+"赢了!您输了!")
System.exit(0)
}
// 斜向右下判断
String youxia = ""
for (int a = 1a <= 5a++) {
youxia += board[xlabel++][ylabel++]
}
if (youxia.equals("●●●●●")) {
System.out.println(roles+"赢了!您输了!")
System.exit(0)
}
}
}
}
}
}
public static void main(String[] args) {
Games wz = new Games()
Scanner sc = new Scanner(System.in)
wz.initBoard()
wz.printBoard()
while (true) {
System.out.print("请"+roles+"输入X,Y坐标,必须在0-15范围内,xy以空格隔开,输入16 16结束程序")
int x = sc.nextInt()
int y = sc.nextInt()
if (x == SIZE &&y == SIZE) {
System.out.println("程序结束")
System.exit(0)
}
if (x >SIZE || x <0 || y >SIZE | y <0) {
System.out.println("输入错误,请从新输入")
continue
}
//如果roles是A玩家 就让A玩家下棋,否则就让B玩家下棋。
if (wz.board[x][y].equals("╋")&&roles.equals("A玩家")) {
wz.board[x][y] = "○"
wz.printBoard()
//判断输赢
wz.whoWin(wz)
}else if(wz.board[x][y].equals("╋")&&roles.equals("B玩家")){
wz.board[x][y] = "●"
wz.printBoard()
//判断输赢
wz.whoWin(wz)
} else {
System.out.println("此处已经有棋子,从新输入")
continue
}
if(roles.equals("A玩家")){
roles = "B玩家"
}else if(roles.equals("B玩家")){
roles = "A玩家"
}
}
}
}
#include<iostream.h>#include<stdlib.h>#define Num 15//********************************************************//类class T//定义类用来封装所有相关函数和变量{ char board[Num][Num]//用数组board[Num][Num]来定义棋盘public: void PrintMenu() //打印菜单 说明游戏规则和方法void PrintBoard() //打印棋盘 void GameStart(char*,int &,int &,char) //下棋 int whichwin(int,int,char) //判断那个选手赢 void Choice(char &) //是否再玩 void Setboard() //重置棋盘}//****************************************************************//main主函数void main ()//主函数{ T s//说明类的一个对象ss.PrintMenu()//通过s调用PrintMenu函数提示如何游戏 char player1[20],player2[20]//玩家姓名 int FirstWin=0,SecondWin=0,Draws=0,x,y,N//说明变量,赋初值为0以待计算输赢结果 char choice='Y' cin.ignore(20,'\n')//输入输出流,前面如果有输入把输入行所有字符取空,以便后面的输入从新的一行开始 cout<<"请输入第一个玩家姓名:" cin.getline(player1,20)//连续读取数据 cout<<"请输入第二个玩家姓名:" cin.getline(player2,20) while(choice=='Y'||choice=='y')//条件成立,执行 { s.Setboard()//调用Setboard函数 N=0 while(N<=(Num*Num)) { s.PrintBoard()//打印棋盘 s.GameStart(player1,x,y,'O') N++//记录已下棋子数 if(s.whichwin(x-1,y-1,'O'))//返回值不为0则条件成立 { s.PrintBoard() cout<<player1<<"赢了。"<<endl FirstWin++//记录赢局数 break//终止本次循环 } s.PrintBoard()//同上 s.GameStart(player2,x,y,'X') N++ if(s.whichwin(x-1,y-1,'X')) { s.PrintBoard() cout<<player2<<"赢了。"<<endl SecondWin++ break } if(N==(Num*Num)) { cout<<"和棋!" Draws++//记录平局数 break } } s.Choice(choice)//给玩家提供一次选择是否再玩的机会 } //输出游戏输赢次数 cout<<player1<<"赢了"<<FirstWin<<"次"<<endl cout<<player2<<"赢了"<<SecondWin<<"次"<<endl cout<<"和"<<Draws<<"次"<<endl cout<<"谢谢使用。"<<endl cout<<"任意键继续。"<<endl cin.get()//很必要的,目的是空度换行字符}//*******************************************************************//定义公有成员函数void T::PrintMenu(){ cout<<"欢迎进入五子棋游戏!\n" cout<<"******************************************"<<endl cout<<"\t游戏说明:"<<endl<<endl cout<<"1.第一个玩家用O第二个玩家用X;"<<endl cout<<"2.请根据提示输入所要走的行和列;"<<endl cout<<"3.按<Enter>下棋。"<<endl cout<<"