问题描述
井井工程。它告诉胜利者是谁。但问题是,当我们玩tic tac toe时,总会有一个平局。所以我想弄清楚如何检查所有9个回合是否都被使用然后打印领带游戏。
为了让它更清楚,让我给你一个例。
假设你是playerOne而我是玩家二。
我们正在玩Tic Tac Toe。
你输入一个数字0.然后我输入1.然后你输入2.然后我输入3依旧.....
在我们提交了所有tic tac toe的盒子并且没有赢家之后它就是一场平局游戏。
所以我的问题是:什么我必须做的是检查所有的盒子是否已满并且没有获胜者所以我们可以有一个平局游戏。 '
如果你需要我的代码。这是官方和完整的代码。
Well Mine Works. It tells who the winner is. But the problem is when we play tic tac toe there will always be a tie. So I am trying to figure it out how I can check if all the 9 turns are used then print tie game.
To make it more clear let me give you an example.
Say you're playerOne and i am player Two.
We are playing Tic Tac Toe.
U enter a number 0. Then I enter 1. Then u enter 2. Then I enter 3 and so on.....
After we've filed all the boxes of tic tac toe and there is no winner then it is a tie game.
So my question to you is: What do I have to do in order to check if all the boxes are full and there is no winner so we can have a Tie Game. '
If you need mine code. Here is the official and full code.
import java.util.Scanner;
public class TicTacToeOffical2{
public static Scanner in = new Scanner(System.in);
public static int Introduction(){
System.out.println("Hey");
System.out.println("Today, you will play the game of Tic Tac Toe");
System.out.println("So, let me go over few rules");
System.out.println("Rule #1: This is how the Tic Tac Toe board will look like. ");
try {
Thread.sleep(1500); //Waits 1.5 Seconds
}catch (Exception e) {
System.out.println("Error");
}
System.out.println(" | | ");
System.out.println(" 0 | 1 | 2 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 3 | 4 | 5 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 6 | 7 | 8 ");
System.out.println(" | | ");
try {
Thread.sleep(1500);
}catch (Exception e) { //Waits 1.5 Seconds
System.out.println("Error");
}
System.out.println("Rule #2: Choose a number to place your variable X or O");
System.out.println("Lets say that you choose to be X and you choose the number 4");
System.out.println("So now, the number 4 will be replaced with X");
System.out.println(" | | ");
System.out.println(" 0 | 1 | 2 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 3 | X | 5 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 6 | 7 | 8 ");
System.out.println(" | | ");
try {
Thread.sleep(2000);
}catch (Exception e) { //Waits 1.5 Seconds
System.out.println("Error");
}
System.out.println("Rule #3: To win the game, you have to get 3 X or O in a row ");
System.out.println(" | | ");
System.out.println(" 0 | X | 2 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 3 | X | 5 ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" 6 | X | 8 ");
System.out.println(" | | ");
try {
Thread.sleep(1500);
}catch (Exception e) { //Waits 1.5 Seconds
System.out.println("Error");
}
System.out.println("So, Would You Like to play Tic Tac Toe?"); // The user will have two options.
System.out.println("Press 1 for yes and 2 for No"); // If the user wants to play, Press 1
int answer = in.nextInt(); // If the user doesn't want to play then press 2
if (answer == 1){
return 2;
}else if (answer == 2){
return 3;
}
return 2;
}
private static int notPlaying(){
System.out.println("So you decided not to play Tic Tac Toe"); // This is the part of the code, when the user
System.out.println("******** THE END **********"); // decides not want to play Tic Tac Toe.
return -1;
}
private static int playTime(){
System.out.println("So, Are you ready to play Tic Tac Toe?");
System.out.println("Player #1 will be X");
System.out.println("Player #2 will be O");
System.out.println("Please wait while your Game is loading........");
try {
Thread.sleep(1000);
}catch (Exception e) { //Waits 1 Seconds
System.out.println("Error");
}
System.out.println("Loading.....");
try {
Thread.sleep(1000);
}catch (Exception e) { //Waits 1 Seconds
System.out.println("Error");
}
System.out.println("Loading.....");
try {
Thread.sleep(1000);
}catch (Exception e) { //Waits 1 Seconds
System.out.println("Error");
}
System.out.println("Loading.....");
try {
Thread.sleep(2000);
}catch (Exception e) { //Waits 1 Seconds
System.out.println("Error");
}
String [] Array = new String [9]; // Created a new string array which can store up to 9 values.
for (int i = 0; i < 9; i++){
Array [i] = " ";
}
for (int i = 0; i < 9; i++){
// Asking Player 1 for a number between 0-8. The number will then be stored
// into the variable called otherPlayer which has an assigned value of O.
System.out.println("Player #1: Please enter a number between 0 to 8");
int currentPlayer = in.nextInt();
if (currentPlayer >8 || currentPlayer <0){
System.out.println("Please enter a valid number");
return 2;
}
Array [currentPlayer] = "X";
// This is an imnporntant code. All the Array represetns a specific number.
// So, when the playerTwo inputs a number, the number gets stored in otherPlayer.
// It will then be then put into an array.
System.out.println(" | | ");
System.out.println(" "+Array[0]+" | " +Array[1]+" | "+Array[2]+" ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" "+Array[3]+" | " +Array[4]+" | "+Array[5]+" ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" "+Array[6]+" | " +Array[7]+" | "+Array[8]+" ");
System.out.println(" | | ");
// This code basically checks for all the combinations of wining Tic Tac Toe.
// For example, the first line will only be true when the user inputs Number 0,1 and 2.
// The program will check for every combinations and then will tell the user if playerTwo has won yet.
if (Array [0] == Array[currentPlayer] && Array [1] == Array[currentPlayer] && Array [2] == Array[currentPlayer]||
(Array [3] == Array[currentPlayer] && Array[4] == Array[currentPlayer] && Array[5] == Array[currentPlayer] ||
(Array [6] == Array[currentPlayer] && Array[7] == Array[currentPlayer] && Array[8] == Array[currentPlayer]||
(Array [0] == Array[currentPlayer] && Array[3] == Array[currentPlayer] && Array[6] == Array[currentPlayer] ||
(Array [1] == Array[currentPlayer] && Array[4] == Array[currentPlayer] && Array[7] == Array[currentPlayer] ||
(Array [2] == Array[currentPlayer] && Array[5] == Array[currentPlayer] && Array[8] == Array[currentPlayer] ||
(Array [0] == Array[currentPlayer] && Array[4] == Array[currentPlayer] && Array[8] == Array[currentPlayer] ||
(Array [2] == Array[currentPlayer] && Array[4] == Array[currentPlayer] && Array[6] == Array[currentPlayer]
)))))))) {
System.out.println("Player One Won");
return -1;
}else if(!Array[0].equals("X") && Array[1].equals("X") && Array[2].equals("X")||
(!Array[3].equals("X") && Array[4].equals("X") && Array[5].equals("X")||
(!Array[6].equals("X") && Array[7].equals("X") && Array[8].equals("X")||
(!Array[0].equals("X") && Array[3].equals("X") && Array[6].equals("X")||
(!Array[1].equals("X") && Array[7].equals("X") && Array[7].equals("X")||
(!Array[2].equals("X") && Array[5].equals("X") && Array[8].equals("X")||
(!Array[0].equals("X") && Array[4].equals("X") && Array[8].equals("X")||
(!Array[2].equals("X") && Array[4].equals("X") && Array[6].equals("X")
)))))))){
System.out.println("Tie Game");
return -1;
}
// Asking Player 2 for a number between 0-8. The number will then be stored
// into the variable called otherPlayer which has an assigned value of O.
System.out.println("Player #2: Please enter a number between 0 to 8");
int otherPlayer = in.nextInt();
Array [otherPlayer] = "O";
// This is the error checking code. So if playerTwo enters
// the same number as playerOne, then playerTwo will be asked again.
// Then, the number will be stored into variable called different number
// which will have the value of O.
if (otherPlayer == currentPlayer){
System.out.println("Sorry, The number you have entered is taken by the other player");
System.out.println("Player #2: Please Enter another number between 0-8");
int anotherNumber = in.nextInt();
Array [anotherNumber] = "O";
Array [currentPlayer] = "X";
}
// This is an imnporntant code. All the Array represetns a specific number.
// So, when the playerTwo inputs a number, the number gets stored in otherPlayer.
// It will then be then put into an array.
System.out.println(" | | ");
System.out.println(" "+Array[0]+" | " +Array[1]+" | "+Array[2]+" ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" "+Array[3]+" | " +Array[4]+" | "+Array[5]+" ");
System.out.println(" | | ");
System.out.println(" -------------------------------");
System.out.println(" | | ");
System.out.println(" "+Array[6]+" | " +Array[7]+" | "+Array[8]+" ");
System.out.println(" | | ");
// This code basically checks for all the combinations of wining Tic Tac Toe.
// For example, the first line will only be true when the user inputs Number 0,1 and 2.
// The program will check for every combinations and then will tell the user if playerTwo has won yet.
if (Array [0] == Array[otherPlayer] && Array [1] == Array[otherPlayer] && Array [2] == Array[otherPlayer]||
(Array [3] == Array[otherPlayer] && Array[4] == Array[otherPlayer] && Array[5] == Array[otherPlayer]||
(Array [6] == Array[otherPlayer] && Array[7] == Array[otherPlayer] && Array[8] == Array[otherPlayer]||
(Array [0] == Array[otherPlayer] && Array[3] == Array[otherPlayer] && Array[6] == Array[otherPlayer]||
(Array [1] == Array[otherPlayer] && Array[4] == Array[otherPlayer] && Array[7] == Array[otherPlayer]||
(Array [2] == Array[otherPlayer] && Array[5] == Array[otherPlayer] && Array[8] == Array[otherPlayer]||
(Array [0] == Array[otherPlayer] && Array[4] == Array[otherPlayer] && Array[8] == Array[otherPlayer]||
(Array [2] == Array[otherPlayer] && Array[4] == Array[otherPlayer] && Array[6] == Array[otherPlayer]
)))))))) {
System.out.println("Player Two Won");
return -1;
} if (!Array[0].equals("O") && Array[1].equals("O") && Array[2].equals("O")||
(!Array[3].equals("O") && Array[4].equals("O") && Array[5].equals("O")||
(!Array[6].equals("O") && Array[7].equals("O") && Array[8].equals("O")||
(!Array[0].equals("O") && Array[3].equals("O") && Array[6].equals("O")||
(!Array[1].equals("O") && Array[7].equals("O") && Array[7].equals("O")||
(!Array[2].equals("O") && Array[5].equals("O") && Array[8].equals("O")||
(!Array[0].equals("O") && Array[4].equals("O") && Array[8].equals("O")||
(!Array[2].equals("O") && Array[4].equals("O") && Array[6].equals("O")
)))))))){
System.out.println("Tie Game");
System.out.println("To Play Again, Please Press 1");
int answer = in.nextInt();
if (answer == 1) {
return 2;
}else{
return 3;
}
}
}
return 1;
}
public static void main(String[] args){ // Main Method
int finish = 1;
while(finish != -1){
if(finish == 1){
finish = Introduction();
}if (finish == 2){
finish = playTime();
}if (finish == 3){
finish = notPlaying();
}
}
}
}
推荐答案
这篇关于如何在Tic Tac Toe中检查这是否是平局游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!