当前位置:编程学习 > JAVA >>

求助大神:如何从电脑上随机输入限定范围的数字

这道题是井字游戏。3*3的格,人和电脑玩,如果谁的棋子连在一起是三个,谁就胜出。
这段代码是两人玩的,一个人用“X”, 另一个用“O”, “X”顺序先于“O”。

想把这段代码改成人和电脑一起玩,人放入棋子后,电脑随机放入一个棋子。不需要用博弈算法。
大概知道使用random实现,求助各位大神,如何实现这个游戏。
如果各位大神有更好的方法,也可以贴出来,一起分享。

谢过了

import java.util.Scanner;
/**
 * Tic-Tac-Toe: Two-player console, non-graphics, non-OO version.
 * All variables/methods are declared as static (belong to the class)
 *  in the non-OO version.
 */
public class Tic {
   // Name-constants to represent the seeds and cell contents
   public static final int EMPTY = 0;
   public static final int CROSS = 1;
   public static final int NOUGHT = 2;
 
   // Name-constants to represent the various states of the game
   public static final int PLAYING = 0;
   public static final int DRAW = 1;
   public static final int CROSS_WON = 2;
   public static final int NOUGHT_WON = 3;
 
   // The game board and the game status
   public static final int ROWS = 3, COLS = 3; // number of rows and columns
   public static int[][] board = new int[ROWS][COLS]; // game board in 2D array
                                                      //  containing (EMPTY, CROSS, NOUGHT)
   public static int currentState;  // the current state of the game
                                    // (PLAYING, DRAW, CROSS_WON, NOUGHT_WON)
   public static int currentPlayer; // the current player (CROSS or NOUGHT)
   public static int currntRow, currentCol; // current seed's row and column
 
   public static Scanner in = new Scanner(System.in); // the input Scanner
 
   /** The entry main method (the program starts here) */
   public static void main(String[] args) {
      // Initialize the game-board and current status
      initGame();
      // Play the game once
      do {
         playerMove(currentPlayer); // update currentRow and currentCol
         updateGame(currentPlayer, currntRow, currentCol); // update currentState
         printBoard();
         // Print message if game-over
         if (currentState == CROSS_WON) {
            System.out.println("'X' won! Bye!");
         } else if (currentState == NOUGHT_WON) {
            System.out.println("'O' won! Bye!");
         } else if (currentState == DRAW) {
            System.out.println("It's a Draw! Bye!");
         }
         // Switch player
         currentPlayer = (currentPlayer == CROSS) ? NOUGHT : CROSS;
      } while (currentState == PLAYING); // repeat if not game-over
   }
 
   /** Initialize the game-board contents and the current states */
   public static void initGame() {
      for (int row = 0; row < ROWS; ++row) {
         for (int col = 0; col < COLS; ++col) {
            board[row][col] = EMPTY;  // all cells empty
         }
      }
      currentState = PLAYING; // ready to play
      currentPlayer = CROSS;  // cross plays first
   }
 
   /** Player with the "theSeed" makes one move, with input validation.
       Update global variables "currentRow" and "currentCol". */
   public static void playerMove(int theSeed) {
      boolean validInput = false;  // for input validation
      do {
         if (theSeed == CROSS) {
            System.out.print("Player 'X', enter your move (row[1-3] column[1-3]): ");
         } else {
            System.out.print("Player 'O', enter your move (row[1-3] column[1-3]): ");
         }
         int row = in.nextInt() - 1;  // array index starts at 0 instead of 1
         int col = in.nextInt() - 1;
         if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {
            currntRow = row;
            currentCol = col;
            board[currntRow][currentCol] = theSeed;  // update game-board content
            validInput = true;  // input okay, exit loop
         } else {
            System.out.println("This move at (" + (row + 1) + "," + (col + 1)
                  + ") is not valid. Try again...");
         }
      } while (!validInput);  // repeat until input is valid
   }
 
   /** Update the "currentState" after the player with "theSeed" has placed on
       (currentRow, currentCol). */
   public static void updateGame(int theSeed, int currentRow, int currentCol) {
      if (hasWon(theSeed, currentRow, currentCol)) {  // check if winning move
         currentState = (theSeed == CROSS) ? CROSS_WON : NOUGHT_WON;
      } else if (isDraw()) {  // check for draw
         currentState = DRAW;
      }
      // Otherwise, no change to currentState (still PLAYING).
   }
 
   /** Return true if it is a draw (no more empty cell) */
   // TODO: Shall declare draw if no player can "possibly" win
   public static boolean isDraw() {
      for (int row = 0; row < ROWS; ++row) {
         for (int col = 0; col < COLS; ++col) {
            if (board[row][col] == EMPTY) {
               return false;  // an empty cell found, not draw, exit
            }
         }
      }
      return true;  // no empty cell, it's a draw
   }
 
   /** Return true if the player with "theSeed" has won after placing at
       (currentRow, currentCol) */
   public static boolean hasWon(int theSeed, int currentRow, int currentCol) {
      return (board[currentRow][0] == theSeed         // 3-in-the-row
                   && board[currentRow][1] == theSeed
                   && board[currentRow][2] == theSeed
              || board[0][currentCol] == theSeed      // 3-in-the-column
                   && board[1][currentCol] == theSeed
                   && board[2][currentCol] == theSeed
              || currentRow == currentCol            // 3-in-the-diagonal
                   && board[0][0] == theSeed
                   && board[1][1] == theSeed
                   && board[2][2] == theSeed
              || currentRow + currentCol == 2  // 3-in-the-opposite-diagonal
                   && board[0][2] == theSeed
                   && board[1][1] == theSeed
                   && board[2][0] == theSeed);
   }
 
   /** Print the game board */
   public static void printBoard() {
      for (int row = 0; row < ROWS; ++row) {
         for (int col = 0; col < COLS; ++col) {
            printCell(board[row][col]); // print each of the cells
            if (col != COLS - 1) {
               System.out.print("|");   // print vertical partition
            }
         }
         System.out.println();
         if (row != ROWS - 1) {
            System.out.println("-----------"); // print horizontal partition
         }
      }
      System.out.println();
   }
 
   /** Print a cell with the specified "content" */
   public static void printCell(int content) {
      switch (content) {
         case EMPTY:  System.out.print("   "); break;
         case NOUGHT: System.out.print(" O "); break;
         case CROSS:  System.out.print(" X "); break;
      }
   }
}
--------------------编程问答-------------------- 如果不用博弈算法。那就很简单的。
9个格子的label对象放到set中,每次从set中取出一个,画上x或O,可以用个标示符。每次判断每条线的情况。
这个线的情况也就6种。
思路就这样 --------------------编程问答--------------------
引用 1 楼 huxiweng 的回复:
如果不用博弈算法。那就很简单的。
9个格子的label对象放到set中,每次从set中取出一个,画上x或O,可以用个标示符。每次判断每条线的情况。
这个线的情况也就6种。
思路就这样


那请问电脑随机输入,这个怎么实现啊~谢啦 --------------------编程问答-------------------- 在 playerMove方法中
在输入一下这两个参数前先判断,如果是人 则输入 ,
int row = in.nextInt() - 1;  // array index starts at 0 instead of 1     
int col = in.nextInt() - 1; 
机器则 随机生成两个数
Random rand = new Random();
int i = rand.nextInt(); //int范围类的随机数
i = rand.nextInt(100); //生成0-100以内的随机数
范围你自己按需设置
int row =i1
int col =i2 --------------------编程问答--------------------
引用 2 楼 Sally_simple 的回复:
Quote: 引用 1 楼 huxiweng 的回复:

如果不用博弈算法。那就很简单的。
9个格子的label对象放到set中,每次从set中取出一个,画上x或O,可以用个标示符。每次判断每条线的情况。
这个线的情况也就6种。
思路就这样


那请问电脑随机输入,这个怎么实现啊~谢啦

在人点击一个格子的时候,点击事件最后从set里取一个(这个就是电脑的输入)绘图 --------------------编程问答--------------------
引用 4 楼 huxiweng 的回复:
Quote: 引用 2 楼 Sally_simple 的回复:

Quote: 引用 1 楼 huxiweng 的回复:

如果不用博弈算法。那就很简单的。
9个格子的label对象放到set中,每次从set中取出一个,画上x或O,可以用个标示符。每次判断每条线的情况。
这个线的情况也就6种。
思路就这样


那请问电脑随机输入,这个怎么实现啊~谢啦

在人点击一个格子的时候,点击事件最后从set里取一个(这个就是电脑的输入)绘图


好的,我清楚了。谢谢了。
还有就是这段代码属于OOP design吗?
我觉得不是,怎么才能用OO design去实现啊? 谢啦,版主~ --------------------编程问答-------------------- 这个纯属是面向过程的,如果需要写成OO的,首先是要把问题抽象出来
就这个问题,可以抽象出三个对象,棋盘、以及两个Player,棋盘负责显示棋盘和棋子,Player负责从棋盘获取数据,然后下棋
main函数属于程序入口,通过main函数将三个对象连接起来 --------------------编程问答--------------------
引用 6 楼 gd920129 的回复:
这个纯属是面向过程的,如果需要写成OO的,首先是要把问题抽象出来
就这个问题,可以抽象出三个对象,棋盘、以及两个Player,棋盘负责显示棋盘和棋子,Player负责从棋盘获取数据,然后下棋
main函数属于程序入口,通过main函数将三个对象连接起来


嗯哪,我知道了。
我有个不情之请,你能不能帮我大概的实现一下啊, 如果你有时间的话。先谢过了。 --------------------编程问答--------------------
引用 7 楼 Sally_simple 的回复:
Quote: 引用 6 楼 gd920129 的回复:

这个纯属是面向过程的,如果需要写成OO的,首先是要把问题抽象出来
就这个问题,可以抽象出三个对象,棋盘、以及两个Player,棋盘负责显示棋盘和棋子,Player负责从棋盘获取数据,然后下棋
main函数属于程序入口,通过main函数将三个对象连接起来


嗯哪,我知道了。
我有个不情之请,你能不能帮我大概的实现一下啊, 如果你有时间的话。先谢过了。

因为我OO 不是很熟悉,一直在学习。但是有个面试又是要求写成OO, 能不能向你学习一下怎么实现它?谢谢
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,