我创建了一个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);
}
}
答案 0 :(得分:1)
为什么当有人获胜时它不会显示最后一个字母。
这种情况正在发生,因为在您的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;
}
}
}