上面规定一定要用function和while 其余的可以自己发挥
Write a program to simulate a simplified version of Connect 4. Connect 4 is a two player game played on a 6x6 game board, where the objective is to drop circular pieces into 6 column slots. The winner is decided when one of the two computer players is able to get 4 circular pieces together horizontally or vertically. This modified version of Connect 4 will be simulated using two computer players.Assume there is no user input for this game. The 6x6 board will be simulated using a 6 x 6 2D array. At the start, the game must randomly decide which computer player (1 or 2) will go first. Then, the program should simulate the game play as follows:• At each turn, the current state of the game board should be displayed
• At each turn, each computer player randomly drops a circular piece into one of the 6 column slots. The column to be dropped into must be randomly decided. Each column slot can only take a maximum of 6 pieces. Then, when a circular piece is dropped in, it will go into a position in that column, which has a x and y value (e.g. [2][2]). If that column is full, then the computer player skips the turn.
• After each circular piece is dropped, the program must check if that computer player has won or not. A win means that after that drop there is either a horizontal or vertical group of 4 circular pieces on the game board.
• If there is no winner, then at the end of the turn, the updated state of the 6x6 2D game board should be displayed.

(0,0)
(1,0) (2,0) (3,0)
(4,0) (5,0)
(0,1) (1,1) (2,1) (3,1)
(4,1) (5,1)
(0,2)
(1,2) (2,2) (3,2) (4,2) (5,2)
(0,3)
(1,3) (2,3) (3,3) (4,3) (5,3)
(0,4)
(1,4) (2,4) (3,4) (4,4) (5,4)
(0,5)
(1,5) (2,5) (3,5) (4,5) (5,5)Overall, each position (or slot) in the displayed 6x6 2D game board can contain any of the following:
• -1  not occupied yet
• 1  occupied by computer player 1’s circular piece
• 2  occupied by computer player 2’s circular pieceThe game ends when one of the following conditions is satisfied:
• Either computer player 1 or 2 has a horizontal or vertical group of 4 pieces on the game board. If this condition is satisfied the program should print that computer player as the winner.
• There are no more slots in the game board to drop pieces onto. Then, the program should print “No winner” as output.Your program must, at least, include the following methods/functions with the correct input parameters and output type:i. initializeGameBoard, which will take as input the player’s 2D array and assign blank slots containing -1 (-1 is assumed to be unoccupied yet).ii. dropPiece, which will drop a circular piece randomly into a slot in one of the six columns.Your program also needs to track and print the following:
• Number of pieces dropped into each column by computer player 1
• Number of pieces dropped into each column by computer player 2

解决方案 »

  1.   

    假设没有user input,用6X6的 2D ARRAY 来模拟6X6的connect 4游戏。开始游戏会随便选择玩家开始。
    每一轮后,当前的状况会显示•在每个回合,每个电脑玩家随机下降到6列插槽的一块圆形。被丢弃到列必须随机决定。每列插槽只能采取最大的6件。然后,被丢弃在一块圆形时,将进入在该列的位置,其中有x和y的值(如[1][2])。如果该列已经满了的,那么电脑玩家跳过回合。
    •每一块圆形下降后,程序必须检查如果该计算机已经赢得与否。胜利是看垂直或平行有没有4个圆形。
    •如果没有赢家,然后在回合结束,6X6 2D游戏板的更新状态应显示。
    (0,0)
    (1,0) (2,0) (3,0)
    (4,0) (5,0)
    (0,1) (1,1) (2,1) (3,1)
    (4,1) (5,1)
    (0,2)
    (1,2) (2,2) (3,2) (4,2) (5,2)
    (0,3)
    (1,3) (2,3) (3,3) (4,3) (5,3)
    (0,4)
    (1,4) (2,4) (3,4) (4,4) (5,4)
    (0,5)
    (1,5) (2,5) (3,5) (4,5) (5,5)
    总的来说,每个显示6x6的2D游戏板的位置(或槽)可以包含任何以下:
    •-1不占用
    •1占用的电脑玩家1的一块圆形
    •2占用的电脑玩家2的一块圆形
    比赛结束时,符合下列条件之一算赢家:
    任何一台计算机1或2的球员有4块游戏板的水平或垂直组。如果这一条件得到满足的程序应该打印,电脑玩家为胜利者。
    •有没有到件下降到在游戏板插槽。然后,该程序应该打印输出“没有赢家”。
    Your program must, at least, include the following methods/functions with the correct input parameters and output type:i. initializeGameBoard, which will take as input the player’s 2D array and assign blank slots containing -1 (-1 is assumed to be unoccupied yet).ii. dropPiece, which will drop a circular piece randomly into a slot in one of the six columns.
    这样行不行 尽力了
      

  2.   

    yes, this is my first assignment ..........
      

  3.   

    randomly决定先手、下法,又没有让你考虑人工智能坦白说,你自己一点代码都不给出来,我也懒得写的
      

  4.   

    对的就是 四子棋 
    坦白说 刚学的 就要叫做这个 不太会做2D array
      

  5.   

    希望你能看得懂$ar = array_chunk(array_fill(0, 36, 0), 6);
    $k = 0;
    for($i=0; $i<18; $i++) {
      if(foo(1)) { $k = 1; break; }
      if(foo(2)) { $k = 2; break; };
    }
    echo "== $k ==\n";
    foreach($ar as $r) echo join('', $r), PHP_EOL;function foo($n) {
      global $ar;
      do {
        $x = rand(0, 5);
        $y = rand(0, 5);
      }while($ar[$x][$y]);
      $ar[$x][$y] = $n;  $k = 0;
      for($i=0; $i<6; $i++) {
        if($ar[$i][$y] == $n) $k++;
        elseif($k) break;
      }
      if($k >= 4) return true;  $k = 0;
      for($i=0; $i<6; $i++) {
        if($ar[$x][$i] == $n) $k++;
        elseif($k) break;
      }
      if($k >= 4) return true;
      return false;
    }