前提・実現したいこと
jAVAでコンソール上で遊べるオセロゲームを作成しています。
入力した座標によって駒を置く場所を決めて順に進めていこうと思っているのですが、
- Board classの List<List<Grid>>の型をBoardという型にして他のクラスなどでも使いたいです。
- 駒を置く時に条件(隣に駒があり、かつ自分の色ではない)に合う場所のみ置けるようにしたいです。
まだまだ初心者なので上記2点わかる方いらっしゃいましたら回答お願いします。
下記に全クラスのコード記載しております。
playerクラス package javaOthello; import java.util.*; public class Player { List<Grid> firstHasStones = new ArrayList<>(); List<Grid> secondHasStones = new ArrayList<>(); public List<Grid> firstHasStones() { firstHasStones.add(Grid.createBlackStone()); return firstHasStones; } public List<Grid> secondHasStones() { secondHasStones.add(Grid.createWhiteStone()); return secondHasStones; } public static List<List<Grid>> putStone(Integer length, Integer side, List<List<Grid>> board, List<Grid> hasStones) { if (board.get(side).get(length).checkEmpty()) { board.get(length).set(side, hasStones.get(0)); } Board.viewBoard(board); return board; } }
Boardクラス package javaOthello; import java.util.*; public class Board { // 盤面の作成 GRID_SIZEを変えるとマス目の数も変更できる Integer GRID_SIZE = 8; public List<List<Grid>> makeBoard() { List<List<Grid>> board = new ArrayList<>(); Player player = new Player(); // 指定したマス目の盤面を作成 for (Integer i = 0; i < GRID_SIZE; i++) { List<Grid> row = new ArrayList<>(); board.add(row); for (Integer n = 0; n < GRID_SIZE; n++) { row.add(Grid.createEmpty()); } } // 石の生成 Grid black = Grid.createBlackStone(); Grid white = Grid.createWhiteStone(); // 真ん中に石を置く board.get(3).set(3, black); board.get(4).set(4, black); board.get(4).set(3, white); board.get(3).set(4, white); // playerに石を持たせる player.firstHasStones(); player.secondHasStones(); viewBoard(board); return board; } public static void viewBoard(List<List<Grid>> board) { System.out.println(); for (List<Grid> row : board) { System.out.println(); for (Grid grid : row) { if (grid.checkEmpty()) { System.out.print("x "); } else { System.out.print(grid + " "); } } } } }
Gridクラス package javaOthello; public class Grid { private String grid; Grid(String black, String white) { if (black == null) { this.grid = white; } if(white == null){ this.grid = black; } } public static Grid createEmpty() { return new Grid(null, null); } public static Grid createWhiteStone() { return new Grid(null, "○"); } public static Grid createBlackStone() { return new Grid("●", null); } public boolean checkEmpty() { return grid == null; } public String getGrid() { return this.grid; } public String toString() { return grid; } }
mainクラス package javaOthello; import java.util.*; public class PlayOthello { public static void main(String[] args) { Board board = new Board(); board.makeBoard(); putStoneNumber(board.makeBoard()); } public static void putStoneNumber(List<List<Grid>> board) { Player player = new Player(); Scanner sc = new Scanner(System.in); boolean exit = false; Integer count = 0; while (!exit) { String enter = sc.nextLine(); Integer length = sc.nextInt(); Integer side = sc.nextInt(); if (enter != null && side != null && length != null) { if (count % 2 == 0) { Player.putStone(length, side, board, player.firstHasStones()); Board.viewBoard(board); } else if (count % 2 != 0) { Player.putStone(length, side, board, player.secondHasStones()); Board.viewBoard(board); } } count++; } } }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。