TicTacToe Game的Java代码 [英] Java code for TicTacToe Game

查看:120
本文介绍了TicTacToe Game的Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用数组等创建一个简单的tic tac toe游戏。
只是一个小项目,我正在为自己工作。
i得到了我想在下面编写代码的想法,但我仍然坚持如何为x或o的每个用户输入更改数组输入。

I am trying to create a simple tic tac toe game as practice with arrays and such. Just a little project i'm working on for myself. i got the idea of how i wanna code it below, but i am stuck on how to get the array input to change for each user input of x or o.

import java.util.Scanner;

public class TicTacToe {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      String[] array;
      array = new String [9]; 

      int uInput;
      int pCounter=1;
      while ( uInput <= 9) {
         System.out.println("Enter X coordinate:");
         uInput = input.nextInt();
         array[uInput] = ("x");

         System.out.println("Enter X coordinate:");
         uInput = input.nextInt();
         array[uInput] = ("o");

         for (int yCounter = 0; yCounter < 6; yCounter++) {
            System.out.print("--");
         }
         System.out.println();

         for (int fCounter=0; fCounter<(3); fCounter++) {
            for (int xCounter=0; xCounter<1; xCounter++) {
               for (pCounter=1; pCounter<4; pCounter++) {   
                  System.out.printf("  %d| ", array[pCounter]);
               }
            } 
         }
         System.out.println();

         for (int zCounter=0; zCounter<4; zCounter++)  {
             System.out.print("---");
         }
         System.out.println();
  }
  System.out.print("0  1  2\n3  4  5\n6  7  8\n");
    }
}


推荐答案

逻辑应该是这样的(伪代码):

The logic should be something like that (pseudo-code):

boolean win = false, tie = false;
String winner = "";
while(!win && !tie){
  getInputFromUser1();
  updateBoard();
  if (win || tie) break;
  getInputFromUser2();
  updateBoard();
}

//check if win or tie and display message respectfully
displayEndMessage();
return;

这篇关于TicTacToe Game的Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆