java實現簡單的五子棋

來源:互聯網
上載者:User
import java.io.*;import java.io.BufferedReader;public class Gobang{private String[][] board;private static int BOARD_SIZE = 15;public void initBoard(){board = new String[BOARD_SIZE][BOARD_SIZE];for(int i = 0; i < BOARD_SIZE; i++){for(int j = 0; j < BOARD_SIZE; j++){board[i][j] = "+";}}}public void printBoard(){for(int i = 0; i < BOARD_SIZE; i++){for(int j = 0; j < BOARD_SIZE; j++){System.out.print(board[i][j]);}System.out.print("\n");}}public static void main(String[] args) throws Exception{Gobang gb = new Gobang();gb.initBoard();gb.printBoard();BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String inputStr = null;while((inputStr = br.readLine()) != null){String[] posStrArr = inputStr.split(",");int xPos = Integer.parseInt(posStrArr[0]);int yPos = Integer.parseInt(posStrArr[1]);gb.board[yPos-1][xPos-1] = "●";/*此處應為電腦隨機產生兩個數,作為電腦下棋的座標,賦給board數組不同與實心圓的白色棋子。涉及:1、座標的有效性,只能是數字,不能超出棋盤的範圍2、下過棋的點,不能重複下棋3、每次下棋後,需要掃描誰贏了*/gb.printBoard();/*並在此進行四次迴圈掃描,判斷橫、豎、左斜、右斜是否有五個棋連在一起。*/System.out.println("請輸入您下棋的座標,應以x,y的格式:");}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.