力扣1275、找出井字棋的获胜者及其扩展!!继续Fighting!!!!!!!!!

A 和 B 在一个 3 x 3 的网格上玩井字棋。
井字棋游戏的规则如下:
玩家轮流将棋子放在空方格 (" ") 上。
第一个玩家 A 总是用 "X" 作为棋子,而第二个玩家 B 总是用 "O" 作为棋子。
"X""O" 只能放在空方格中,而不能放在已经被占用的方格上。
只要有 3 个相同的(非空)棋子排成一条直线(行、列、对角线)时,游戏结束。
如果所有方块都放满棋子(不为空),游戏也会结束。
游戏结束后,棋子无法再进行任何移动。
给你一个数组 moves,其中每个元素是大小为 2 的另一个数组(元素分别对应网格的行和列),它按照 A 和 B 的行动顺序(先 A 后 B)记录了两人各自的棋子位置。
如果游戏存在获胜者(A 或 B),就返回该游戏的获胜者;如果游戏以平局结束,则返回 "Draw";如果仍会有行动(游戏未结束),则返回 "Pending"。
你可以假设 moves 都 有效(遵循井字棋规则),网格最初是空的,A 将先行动。
示例 1:
输入:moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
输出:"A"
解释:"A" 获胜,他总是先走。
"X  "    "X  "    "X  "    "X  "    "X  "
"   " -> "   " -> " X " -> " X " -> " X "
"   "    "O  "    "O  "    "OO "    "OOX"
示例 2:
输入:moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
输出:"B"
解释:"B" 获胜。
"X  "    "X  "    "XX "    "XXO"    "XXO"    "XXO"
"   " -> " O " -> " O " -> " O " -> "XO " -> "XO " 
"   "    "   "    "   "    "   "    "   "    "O  "
示例 3:
输入:moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
输出:"Draw"
输出:由于没有办法再行动,游戏以平局结束。
"XXO"
"OOX"
"XOX"
示例 4:
输入:moves = [[0,0],[1,1]]
输出:"Pending"
解释:游戏还没有结束。
"X  "
" O "
"   "
提示:
1 <= moves.length <= 9
moves[i].length == 2
0 <= moves[i][j] <= 2
moves 里没有重复的元素。
moves 遵循井字棋的规则。

这一道题目的难度不是很大,主要就是判断8种情况,竖着的有3种,横着胜利的有3种,斜着胜利的两种。综上所述我们需要对传入的数组放置好棋子后进行,判断是否胜利,或者是否还未完成,没有地方下棋子啦

于是代码如下:

package medium;

public class tictac {
	public String tictactoe(int[][] moves) {
		String[] s = new String[3];
		s = newplace(s, 3);
		for (int i = 0; i < moves.length; i++) {
			int[] l = moves[i];
			int height = l[0];
			int width = l[1];
			StringBuilder sb = new StringBuilder(s[height]);
			if (i % 2 == 0) {

				sb.replace(width, width + 1, "X");

			} else {
				sb.replace(width, width + 1, "O");
			}
			s[height] = sb.toString();
		}
		for (int i = 0; i < s.length; i++) {
			System.out.println(s[i]);
		}
		
		int isWin = WinOrNot(s);
		if(moves.length==9&&isWin<0)
			return "由于没有办法再行动,游戏以平局结束。";
		else if(isWin==0)
			return "A";
		else if(isWin==1)
			return "B";
		else if(moves.length<9&&isWin<0)
			return "游戏还没有结束。";

		return null;

	}

	private int WinOrNot(String[] s) {
	   char[][] temp=new char[3][3];
	   temp[0]=s[0].toCharArray();
	   temp[1]=s[1].toCharArray();
	   temp[2]=s[2].toCharArray();
		  if(temp[0][0]!=' '){
			  char x=temp[0][0];
			  if((temp[1][0]==x&&temp[2][0]==x)||(temp[1][1]==x&&temp[2][2]==x)||(temp[0][1]==x&&temp[0][2]==x)){
				  int re= x=='X'? 0:1;
				  return re;
			  }
		  }
		   if(temp[0][2]!=' '){
			  char x=temp[0][2];
			  if((temp[1][2]==x&&temp[2][2]==x)||(temp[1][1]==x&&temp[2][0]==x)||(temp[0][1]==x&&temp[0][0]==x)){
				  int re= x=='X'? 0:1;
				  return re;
			  }
		  }
		   if(temp[0][1]!=' '){
			  char x=temp[0][1];
			  if(temp[1][1]==x&&temp[2][1]==x){
				  int re= x=='X'? 0:1;
				  return re;
			  }
		  }
		   if(temp[1][0]!=' '){
			  char x=temp[1][0];
			  if(temp[1][1]==x&&temp[1][2]==x){
				  int re= x=='X'? 0:1;
				  return re;
			  }
		  }
		   if(temp[2][0]!=' '){
			  char x=temp[2][0];
			  if(temp[2][1]==x&&temp[2][2]==x){
				  int re= x=='X'? 0:1;
				  return re;
			  }
		  }
		   
		   
	   
		return -1;
	}

	private String[] newplace(String[] s, int i) {
		for (int j = 0; j < s.length; j++) {
			String temp = "";
			for (int k = 0; k < i; k++) {
				temp = temp + " ";
			}
			s[j] = temp;
		}
		return s;
	}

	public static void main(String[] args) {
		int[][] moves = { {1,2}, { 0,2}, { 0, 0 }, {1,0 }, {2,0} };
		tictac t = new tictac();
		System.out.println(t.tictactoe(moves));
	}

}

但是说实话,我对于这个不太满意,于是我决定将其升级Up。
从控制台输入,然后进行交互,就像真正的游戏那样两人操作。同时为了节约时间
我们不可能每次输入都判断,这是极不理智的
我们根据常识可以得知,当两个人下到有一方获胜的最小步数是5步
于是代码如下:

package medium;

import java.util.Scanner;

public class myTic {
	String[] s = new String[3];
	public int tictactoe() {
		Scanner sc=new Scanner(System.in);
		
		s = newplace(s, 3);
		printplace(s);
		int isWin = WinOrNot(s);
		int move=1;//一共有9步
		while(isWin<0&&move<=9){
			if(move%2==0)
			  System.out.println("请玩家二走第"+move+"步");
			else 
				System.out.println("请玩家一走第"+move+"步");
			int[] temp=new int[2];
			System.out.println("第"+move+"步的行");
			temp[0]=sc.nextInt()-1;
			System.out.println("第"+move+"步的列");
			temp[1]=sc.nextInt()-1;
			if(!isExist(temp)){
				int height = temp[0];
				int width =temp[1];
				StringBuilder sb = new StringBuilder(s[height]);
				if (move % 2 != 0) {
					sb.replace(width, width + 1, "X");
				} else {
					sb.replace(width, width + 1, "O");
				}
				s[height] = sb.toString();
				if(move>=5)
					isWin=WinOrNot(s);
				move++;
				printplace(s);
			}else{
				System.out.println("重复,请重新走");
			}
		}
		return isWin;

	}


	private boolean isExist(int[] temp) {
		int w=temp[0];
		int l=temp[1];
		if(s[w].charAt(l)=='.')
			return false;
		else 
		return true;
	}


	private void printplace(String[] s) {
		for (int i = 0; i < s.length; i++) {
			System.out.println(s[i]);
		}
	}

	private int WinOrNot(String[] s) {
	   char[][] temp=new char[3][3];
	   temp[0]=s[0].toCharArray();
	   temp[1]=s[1].toCharArray();
	   temp[2]=s[2].toCharArray();
		  if(temp[0][0]!='.'){
			  char x=temp[0][0];
			  if((temp[1][0]==x&&temp[2][0]==x)||(temp[1][1]==x&&temp[2][2]==x)||(temp[0][1]==x&&temp[0][2]==x)){
				  int re= x=='X'? 0:1;
				  return re;
			  }
		  }
		   if(temp[0][2]!='.'){
			  char x=temp[0][2];
			  if((temp[1][2]==x&&temp[2][2]==x)||(temp[1][1]==x&&temp[2][0]==x)||(temp[0][1]==x&&temp[0][0]==x)){
				  int re= x=='X'? 0:1;
				  return re;
			  }
		  }
		   if(temp[0][1]!='.'){
			  char x=temp[0][1];
			  if(temp[1][1]==x&&temp[2][1]==x){
				  int re= x=='X'? 0:1;
				  return re;
			  }
		  }
		   if(temp[1][0]!='.'){
			  char x=temp[1][0];
			  if(temp[1][1]==x&&temp[1][2]==x){
				  int re= x=='X'? 0:1;
				  return re;
			  }
		  }
		   if(temp[2][0]!='.'){
			  char x=temp[2][0];
			  if(temp[2][1]==x&&temp[2][2]==x){
				  int re= x=='X'? 0:1;
				  return re;
			  }
		  }
		return -1;
	}

	private String[] newplace(String[] s, int i) {
		for (int j = 0; j < s.length; j++) {
			String temp = "";
			for (int k = 0; k < i; k++) {
				temp = temp + ".";
			}
			s[j] = temp;
		}
		return s;
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
		myTic m=new myTic();
		int i=m.tictactoe();
		if(i==0){
			System.out.println("玩家一获胜");
		}else if(i==1){
			System.out.println("玩家二获胜");
		}
		else if(i<0){
			System.out.println("平局");
		}

	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章