Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串与另一个字符串重叠_Java_Arrays_String_Tic Tac Toe - Fatal编程技术网

Java 字符串与另一个字符串重叠

Java 字符串与另一个字符串重叠,java,arrays,string,tic-tac-toe,Java,Arrays,String,Tic Tac Toe,我知道我的代码有点乱,但我会尽量详细。代码正在创建一个TicTacToe游戏。我正在使用数组创建游戏板,即gameBoard()。用户总是X,而计算机总是O。我已经为用户输入和计算机输入创建了if语句,不同的是,计算机从java.util.Random获取其编号。if语句获取用户的输入,并在指定的板上打印一个X,与计算机相同。这里的问题是,当随机生成器选择一个已经存在X的空间时,它会重叠,反之亦然。我需要这个程序秘密地告诉计算机生成另一个随机数。如果你能帮忙,那就太好了。抱歉这么大的描述 imp

我知道我的代码有点乱,但我会尽量详细。代码正在创建一个TicTacToe游戏。我正在使用数组创建游戏板,即gameBoard()。用户总是X,而计算机总是O。我已经为用户输入和计算机输入创建了if语句,不同的是,计算机从java.util.Random获取其编号。if语句获取用户的输入,并在指定的板上打印一个X,与计算机相同。这里的问题是,当随机生成器选择一个已经存在X的空间时,它会重叠,反之亦然。我需要这个程序秘密地告诉计算机生成另一个随机数。如果你能帮忙,那就太好了。抱歉这么大的描述

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Random;

public class TicTacToeGame {

static String arrayPicture[][]; //Array is universal so we can include it into any method
static int userInput;
static boolean validInput = false;
static String name;

public static void main (String[] args) throws IOException {

    System.out.println ("Please enter your name:");
    name = gameStart("");

    arrayPicture = new String [13][13];

    for (int i = 0; i < 13; i ++) {

        for (int j = 0; j < 13; j  ++) {
            arrayPicture [i][j] = " "; //Print spaces into the array
        }   
    }       

    while (validInput == false) {

        validX();

        cpuPlacement();

        System.out.println ("Computer, please enter your number");
        validInput = false;
    }       
}//main

public static void verticalLine (int x1, int y1, int x2) {

    for (int k = x1; k < x2; k ++) {
        arrayPicture [y1][k] = "|"; //if y1 and k are present, print a vertical line in the array
    }       
}//verticalLine

public static void horizontalLine (int x1, int y1, int x2) {
    for (int k = x1; k < x2; k ++) {
        arrayPicture [y1][k] = "-"; //if y1 and k are present, print a dash in the array
    }       
}//horizontalLine

public static void printArray () {
    for (int i = 0; i < 13; i ++) {

        for (int j = 0; j < 13; j ++) {
            System.out.print (arrayPicture[i][j]);
        }   
        System.out.println();
    }       
}//printArray

public static String gameStart (String input) throws IOException { 

        boolean validInput = false;

        BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in));

        while (validInput == false) {
            input = userInput.readLine();
            validInput = true;

                System.out.println ("The name of the game is TicTacToe, you will always be the letter X.");
                System.out.println ("Please choose the coordinates you wish. The coordinates are where your X will be placed on the game board.");

        }
        return input;
}

public static void validX () throws IOException {

    gameBoard();

    String input = null;

    System.out.println (name + ", please enter your number");

        try {
            BufferedReader columnNumber = new BufferedReader (new InputStreamReader(System.in));//BufferedReader variable where the user will input their answer
            input = columnNumber.readLine();
            userInput = Integer.parseInt (input);
            validInput = true;
            userPlacement();
            //If an exception is present, the try sends it to catch.
            //Ex. If the user in\puts "bob has a cat" the try will find an exception and immediately send it to the catch statement
        }
            catch (NumberFormatException ex) {
            System.out.println ("Invalid input, please enter a number.");
            validInput = false;
        }

        if (userInput == 1) {
            userInputX (2, 2, 3);
        }
        else if (userInput == 2) {
            userInputX (6, 2, 7);
        }
        else if (userInput == 3) {
            userInputX (10, 2, 11);
        }
        else if (userInput == 4) {
            userInputX (2, 6, 3);
        }
        else if (userInput == 5) {
            userInputX (6, 6, 7);
        }
        else if (userInput == 6) {
            userInputX (10, 6, 11);
        }
        else if (userInput == 7) {
            userInputX (2, 10, 3);
        }
        else if (userInput == 8) {
            userInputX (6, 10, 7);
        }                               
        else if (userInput == 9) {
            userInputX (10, 10, 11);
        }
}

public static void cpuinputO (int x1, int y1, int x2) throws IOException {

    gameBoard();

    for (int k = x1; k < x2; k ++) {
        arrayPicture [y1][k] = "O"; //if y1 and k are present, print a dash in the array
    }
}

public static void userInputX (int x1, int y1, int x2) {

    for (int k = x1; k < x2; k ++) {
        arrayPicture [y1][k] = "X"; //if y1 and k are present, print a dash in the array
    }
}

public static void userPlacement () throws IOException {

    if (userInput == 1) {
        userInputX (2, 2, 3);
    }
    else if (userInput == 2) {
        userInputX (6, 2, 7);
    }
    else if (userInput == 3) {
        userInputX (10, 2, 11);
    }
    else if (userInput == 4) {
        userInputX (2, 6, 3);
    }
    else if (userInput == 5) {
        userInputX (6, 6, 7);
    }
    else if (userInput == 6) {
        userInputX (10, 6, 11);
    }
    else if (userInput == 7) {
        userInputX (2, 10, 3);
    }
    else if (userInput == 8) {
        userInputX (6, 10, 7);
    }                               
    else if (userInput == 9) {
        userInputX (10, 10, 11);
    }
}

public static void cpuPlacement () throws IOException {

    Random random = new Random();
    int cpuCoordinates = random.nextInt(10);
    System.out.println (cpuCoordinates);
    boolean validInput0 = false;

    if (validInput0 == true) {

        if (cpuCoordinates < 1) {
            validInput0 = false;
        }
    }
    else if (cpuCoordinates == 1) {
        cpuinputO (2, 2, 3);
    }
    else if (cpuCoordinates == 2) {
        cpuinputO (6, 2, 7);
    }
    else if (cpuCoordinates == 3) {
        cpuinputO (10, 2, 11);
    }
    else if (cpuCoordinates == 4) {
        cpuinputO (2, 6, 3);
    }
    else if (cpuCoordinates == 5) {
        cpuinputO (6, 6, 7);
    }
    else if (cpuCoordinates == 6) {
        cpuinputO (10, 6, 11);
    }
    else if (cpuCoordinates == 7) {
        cpuinputO (2, 10, 3);
    }
    else if (cpuCoordinates == 8) {
        cpuinputO (6, 10, 7);
    }                               
    else if (cpuCoordinates == 9) {
        cpuinputO (10, 10, 11);
    }
}

public static void gameBoard () throws IOException {
    System.out.println (" ");
    horizontalLine (1, 0, 4);  horizontalLine (5, 0, 8);  horizontalLine (9, 0, 12);
    verticalLine (0, 1, 1);  verticalLine (4, 1, 5);  verticalLine (8, 1, 9);  verticalLine (12, 1, 13);
    verticalLine (0, 2, 1);  verticalLine (4, 2, 5);  verticalLine (8, 2, 9);  verticalLine (12, 2, 13);
    verticalLine (0, 3, 1);  verticalLine (4, 3, 5);  verticalLine (8, 3, 9);  verticalLine (12, 3, 13);
    horizontalLine (1, 4, 4);  horizontalLine (5, 4, 8);  horizontalLine (9, 4, 12);
    verticalLine (0, 5, 1);  verticalLine (4, 5, 5);  verticalLine (8, 5, 9);  verticalLine (12, 5, 13);
    verticalLine (0, 6, 1);  verticalLine (4, 6, 5);  verticalLine (8, 6, 9);  verticalLine (12, 6, 13);
    verticalLine (0, 7, 1);  verticalLine (4, 7, 5);  verticalLine (8, 7, 9);  verticalLine (12, 7, 13);
    horizontalLine (1, 8, 4);  horizontalLine (5, 8, 8);  horizontalLine (9, 8, 12);
    verticalLine (0, 9, 1);  verticalLine (4, 9, 5);  verticalLine (8, 9, 9);  verticalLine (12, 9, 13);
    verticalLine (0, 10, 1); verticalLine (4, 10, 5);  verticalLine (8, 10, 9);  verticalLine (12, 10, 13);
    verticalLine (0, 11, 1); verticalLine (4, 11, 5);  verticalLine (8, 11, 9);  verticalLine (12, 11, 13);
    horizontalLine (1, 12, 4);  horizontalLine (5, 12, 8);  horizontalLine (9, 12, 12);
    printArray ();

 }//gameBoard
}//public class
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
导入java.io.IOException;
导入java.util.Random;
公共类游戏{
静态字符串arrayPicture[][];//数组是通用的,因此我们可以将其包含到任何方法中
静态int用户输入;
静态布尔值validInput=false;
静态字符串名;
公共静态void main(字符串[]args)引发IOException{
System.out.println(“请输入您的姓名:”);
名称=游戏开始(“”);
arrayPicture=新字符串[13][13];
对于(int i=0;i<13;i++){
对于(int j=0;j<13;j++){
arrayPicture[i][j]=“”;//将空格打印到数组中
}   
}       
while(validInput==false){
validX();
cpuPlacement();
System.out.println(“计算机,请输入您的号码”);
validInput=假;
}       
}//主要
公共静态无效垂直线(int x1、int y1、int x2){
对于(int k=x1;kwhile(validInput0==false){
    if(cpuCoordinates == 1){
        if(squareOneOwner == null){
            squareOneOwner = "CPU"
            validInput0 = true;
        }
    }else if(...){
        ...
    }
    if(validInput0 == false){
        cpuCoordinates = random.nextInt(10);
    }
}
int[][] game_board = new int[3][3]

for(int i = 0; i < 3; i++)
{
  for(int j = 0; j < 3; j++)
  {
     if(game_board[i][j] == 1)
        ...code to print an X...
     if(game_board[i][j] == 2)
        ...code to print an O...
     if(game_board[i][j] == 0)
        ...code to print some spaces...

     ...code to print a vertical line...
  }

  ...code to print a horizontal line...
}
choice_x = ...user or computer chooses an x location...
choice_y = ...user or computer chooses a  y location...

if(game_board[choice_x][choice_y] != 0)
{
    ...choose again...
}
else
{
    if( player is choosing ) 
        game_board[choice_x][choice_y] = 1 
    if( computer is choosing )
        game_board[choice_x][choice_y] = 0
}