Guest User

Untitled

a guest
Feb 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. public class Driver {
  2. public static void main(String args[]) {
  3. Piece player1 = new Piece('o');
  4. Piece player2 = new Piece('x');
  5. Piece player3 = new Piece('v');
  6. Piece players[] = {player1, player2, player3};
  7. Game game = new Game(players);
  8. game.board.printBoard();
  9. System.out.println("Type 1, 2, or 3 to move a piece, type q to quit.");
  10. Scanner scan = new Scanner (System.in);
  11. boolean running = true;
  12. while(running) {
  13. String input = scan.next();
  14. if(input.equals("1")){
  15. game.movePiece(player1);
  16. }else if(input.equals("2")){
  17. game.movePiece(player2);
  18. }else if(input.equals("3")){
  19. game.movePiece(player3);
  20. }
  21. game.board.printBoard();
  22. if(game.hasWinner) {
  23. System.out.println(game.winner.icon + " wins!");
  24. running=false;
  25. }
  26. if(input.equals("q")) {
  27. running = false;
  28. }
  29. }
  30. }
  31. }
  32.  
  33. public class Board {
  34. int size; //board is square, size in both dimensions
  35. Piece tile[][]; //holds pieces, spots are null when empty
  36.  
  37. public Board(int size) {
  38. this.size = size;
  39. tile = new Piece[size][size];
  40. }
  41.  
  42. public void printBoard() {
  43. //print the column header
  44. System.out.print(" ");
  45. for(int col=0; col<size; col++) {
  46. System.out.print(col + " ");
  47. }
  48. System.out.println();
  49.  
  50. //print the board and row header
  51. for(int row=0; row<size; row++) {
  52. System.out.print(row + " ");
  53. for(int col=0; col<size; col++) {
  54. if(tile[row][col]==null) {
  55. System.out.print("- ");
  56. }else {
  57. System.out.print(tile[row][col].icon + " ");
  58. }
  59. }
  60. System.out.println();
  61. }
  62. System.out.println();
  63. }
  64. }
  65.  
  66. public class Game {
  67. public Piece player;
  68. public Board board;
  69. public int players;
  70.  
  71. public Game(Piece player[]) {
  72. board= new Board(8);
  73. this.player[1]=player[players];
  74. for(int i=0; i<player.length;i++) {
  75. player[0]=board.tile[0][0];
  76.  
  77. }
  78.  
  79. }
  80.  
  81. public void movePiece(Piece player) {
  82. this.player= player;
  83.  
  84. }
  85. }
  86.  
  87. public class Piece {
  88.  
  89. char icon;
  90. int x;
  91. int y;
  92.  
  93. public Piece(char icon) {
  94. this.icon = icon;
  95.  
  96.  
  97. }
  98.  
  99. }
Add Comment
Please, Sign In to add comment