gpt4 book ai didi

java - TicTacToe 不显示所有字母

转载 作者:行者123 更新时间:2023-12-01 10:55:13 28 4
gpt4 key购买 nike

我创建了一个 TicTacToe 游戏,但我不明白为什么当有人获胜时它不会显示最后一个字母。就像如果您连续获得 3 个字母,它将仅显示 2 个字母,但不会显示获胜字母(如果这有意义的话)。

TicTacToe 类代码:

import java.util.Scanner;
import javax.swing.JFrame;

public class TicTacToe {
public static void main(String [] args) {
Board board = new Board();
Rules winner = new Rules();

Scanner input = new Scanner(System.in);

int slot;
int swap = 1;

System.out.print("Player 1 Name: ");
String player1 = input.next();
System.out.print('\f');
System.out.print("Player 2 Name: ");
String player2 = input.next();
System.out.print('\f');

System.out.print(player1 + ", Choose X or O: ");
String letter = input.next();
System.out.print('\f');

String player1Letter;
String player2Letter;

if (letter.equalsIgnoreCase("x")) {
player1Letter = "X";
player2Letter = "O";
} else {
player1Letter = "O";
player2Letter = "X";
}

System.out.println("This is how each number is set on the board");
System.out.println();
System.out.println(" 1 | 2 | 3 ");
System.out.println("-------------");
System.out.println(" 4 | 5 | 6 ");
System.out.println("--------- ---");
System.out.println(" 7 | 8 | 9 ");
System.out.println();
System.out.println("Each number represents the slot number. For example if you wanted to");
System.out.println("Place an 'X' or an 'O' in the very middle, you would choose slot 5");
System.out.println();

while (true) {
System.out.print("Type 'begin' to begin: ");
String begin = input.next();
if (begin.equalsIgnoreCase("begin")) {
break;
} else if (!begin.equalsIgnoreCase("begin")) {
System.out.println();
System.out.println("Incorrect Syntax");
}
}
while (true) {
System.out.print('\f');
System.out.println(" " + board.getPosition(1) + " | " + board.getPosition(2) + " | " + board.getPosition(3) + " ");
System.out.println("----------------");
System.out.println(" " + board.getPosition(4) + " | " + board.getPosition(5) + " | " + board.getPosition(6) + " ");
System.out.println("----------------");
System.out.println(" " + board.getPosition(7) + " | " + board.getPosition(8) + " | " + board.getPosition(9) + " ");
System.out.println();
System.out.print("Choose A Slot: ");
slot = input.nextInt();
if (swap == 1 || swap == 3 || swap == 5 || swap == 7 || swap == 9 || swap == 11) {
board.setPosition(slot, player1Letter);
swap++;
} else if (swap == 2 || swap == 4 || swap == 6 || swap == 8 || swap == 10 || swap == 12) {
board.setPosition(slot, player2Letter);
swap++;
}
if (winner.isWinner(board) == true) {
if (swap == 1 || swap == 3 || swap == 5 || swap == 7 || swap == 9 || swap == 11) { //swap%2 == 1
System.out.println();
System.out.print(player2 + " Wins!");
break;
}
if (swap == 2 || swap == 4 || swap == 6 || swap == 8 || swap == 10 || swap == 12) {
System.out.println();
System.out.print(player1 + " Wins!");
break;
} else if (winner.isWinner(board) == false) {
System.out.println();
System.out.print("Tie Game");
break;
}
}
}
if (winner.isWinner(board) == true) {
JFrame frame = new JFrame();
frame.setSize(150, 250);
frame.setTitle("A Simley Face");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

FaceComponent component = new FaceComponent();
frame.add(component);

frame.setVisible(true);
}
}
}

板类代码:

public class Board
{
private String position;
String topLeft= "1";
String topMiddle= "2";
String topRight= "3";
String middleLeft= "4";
String middleMiddle= "5";
String middleRight= "6";
String bottomLeft= "7";
String bottomMiddle= "8";
String bottomRight= "9";
public String getPosition(int pos)
{
if (pos == 1)
{
return topLeft;//varible of position 1
}
else if (pos == 2)
{
return topMiddle;//varible of position 2
}
else if (pos == 3)
{
return topRight;//varible of position 3
}
else if (pos == 4)
{
return middleLeft;//varible of position 4
}
else if (pos == 5)
{
return middleMiddle;//varible of position 5
}
else if (pos == 6)
{
return middleRight;//varible of position 6
}
else if (pos == 7)
{
return bottomLeft;//varible of position 7
}
else if (pos == 8)
{
return bottomMiddle;//varible of position 8
}
else if (pos == 9)
{
return bottomRight;//varible of position 9
}
return position;
}
public String setPosition(int pos, String value)
{
if (pos == 1)
{
topLeft = value;
}
else if (pos == 2)
{
topMiddle = value;
}
else if (pos == 3)
{
topRight = value;
}
else if (pos == 4)
{
middleLeft = value;
}
else if (pos == 5)
{
middleMiddle = value;
}
else if (pos == 6)
{
middleRight = value;
}
else if (pos == 7)
{
bottomLeft = value;
}
else if (pos == 8)
{
bottomMiddle = value;
}
else if (pos == 9)
{
bottomRight = value;
}
return position;
}
}

规则类代码:

public class Rules
{
private String winner;
private String threeInARow;

public boolean isWinner(Board threeInARow)
{
String pos1 = threeInARow.getPosition(1);
String pos2 = threeInARow.getPosition(2);
String pos3 = threeInARow.getPosition(3);
String pos4 = threeInARow.getPosition(4);
String pos5 = threeInARow.getPosition(5);
String pos6 = threeInARow.getPosition(6);
String pos7 = threeInARow.getPosition(7);
String pos8 = threeInARow.getPosition(8);
String pos9 = threeInARow.getPosition(9);

if (pos1.equals(pos2) && pos2.equals(pos3))
{
return true;
}
else if (pos4.equals(pos5) && pos5.equals(pos6))
{
return true;
}
else if (pos7.equals(pos8) && pos8.equals(pos9))
{
return true;
}
else if (pos1.equals(pos4) && pos4.equals(pos7))
{
return true;
}
else if (pos2.equals(pos5) && pos5.equals(pos8))
{
return true;
}
else if (pos3.equals(pos6) && pos6.equals(pos9))
{
return true;
}
else if (pos1.equals(pos5) && pos5.equals(pos9))
{
return true;
}
else if (pos3.equals(pos5) && pos5.equals(pos7))
{
return true;
}
else
{
return false;
}

}
}

FaceComponent类代码:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
public class FaceComponent extends JComponent
{
public void paintComponent(Graphics g)
{
// Recover Graphics2D
Graphics2D g2 = (Graphics2D) g;

// Draw the head
Ellipse2D.Double head = new Ellipse2D.Double(5, 10, 100, 150);
g2.draw(head);

// Draw the eyes
g2.setColor(Color.BLACK);
Rectangle eye = new Rectangle(25, 70, 15, 15);
g2.fill(eye);
eye.translate(50, 0);
g2.fill(eye);

// Draw the mouth
Line2D.Double mouth = new Line2D.Double(30, 110, 80, 110);
g2.setColor(Color.RED);
g2.draw(mouth);

// Draw the greeting
g2.setColor(Color.BLUE);
g2.drawString("YOU ARE A WINNER", 5, 175);
}
}

最佳答案

why it won't display the last letter when someone wins.

发生这种情况是因为在您的 main 方法中,在收到用户输入后,您正在将用户选择的位置更改为播放器的字母。然后您将继续检查玩家是否获胜。如果玩家没有获胜,那么您将继续抽牌并等待下一个玩家输入。但如果玩家获胜,那么您只需显示消息并退出循环,而无需重新绘制棋盘。

解决方案是您可以在检查获胜者的 if 条件内再次绘制棋盘,然后显示消息并退出循环。

我建议你在你的类TicTacToe中声明一个方法,比如画板,如下所示:

private void drawBoard(Board board){
System.out.print('\f');
System.out.println(" " + board.getPosition(1) + " | " + board.getPosition(2) + " | " + board.getPosition(3) + " ");
System.out.println("----------------");
System.out.println(" " + board.getPosition(4) + " | " + board.getPosition(5) + " | " + board.getPosition(6) + " ");
System.out.println("----------------");
System.out.println(" " + board.getPosition(7) + " | " + board.getPosition(8) + " | " + board.getPosition(9) + " ");
System.out.println();
}

现在更改您的 while 循环以绘制板并从以下位置获取用户输入:

      while (true) {
System.out.print('\f');
System.out.println(" " + board.getPosition(1) + " | " + board.getPosition(2) + " | " + board.getPosition(3) + " ");
System.out.println("----------------");
System.out.println(" " + board.getPosition(4) + " | " + board.getPosition(5) + " | " + board.getPosition(6) + " ");
System.out.println("----------------");
System.out.println(" " + board.getPosition(7) + " | " + board.getPosition(8) + " | " + board.getPosition(9) + " ");
System.out.println();
System.out.print("Choose A Slot: ");
slot = input.nextInt();
if (swap == 1 || swap == 3 || swap == 5 || swap == 7 || swap == 9 || swap == 11) {
board.setPosition(slot, player1Letter);
swap++;
} else if (swap == 2 || swap == 4 || swap == 6 || swap == 8 || swap == 10 || swap == 12) {
board.setPosition(slot, player2Letter);
swap++;
}
if (winner.isWinner(board) == true) {
if (swap == 1 || swap == 3 || swap == 5 || swap == 7 || swap == 9 || swap == 11) { //swap%2 == 1
System.out.println();
System.out.print(player2 + " Wins!");
break;
}
if (swap == 2 || swap == 4 || swap == 6 || swap == 8 || swap == 10 || swap == 12) {
System.out.println();
System.out.print(player1 + " Wins!");
break;
} else if (winner.isWinner(board) == false) {
System.out.println();
System.out.print("Tie Game");
break;
}
}
}

至:

      while (true) {
drawBoard(board); //Replace with draw board
System.out.print("Choose A Slot: ");
slot = input.nextInt();
if (swap == 1 || swap == 3 || swap == 5 || swap == 7 || swap == 9 || swap == 11) {
board.setPosition(slot, player1Letter);
swap++;
} else if (swap == 2 || swap == 4 || swap == 6 || swap == 8 || swap == 10 || swap == 12) {
board.setPosition(slot, player2Letter);
swap++;
}
if (winner.isWinner(board) == true) {
drawBoard(board); // Add the draw board.
if (swap == 1 || swap == 3 || swap == 5 || swap == 7 || swap == 9 || swap == 11) { //swap%2 == 1
System.out.println();
System.out.print(player2 + " Wins!");
break;
}
if (swap == 2 || swap == 4 || swap == 6 || swap == 8 || swap == 10 || swap == 12) {
System.out.println();
System.out.print(player1 + " Wins!");
break;
} else if (winner.isWinner(board) == false) {
System.out.println();
System.out.print("Tie Game");
break;
}
}
}

关于java - TicTacToe 不显示所有字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33643727/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com