Guest User

Untitled

a guest
Nov 13th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. Jacks-MacBook-Pro:Cs124 Jack_XXX$
  2.  
  3. public static void main(String[] args) {
  4.  
  5. ConnectFour connectFour= new ConnectFour();
  6. BoardView view = connectFour.getView();
  7.  
  8. JFrame worldFrame = new JFrame();
  9. JPanel buttonPanel = new JPanel();
  10.  
  11.  
  12. view.repaint();
  13.  
  14. worldFrame.setContentPane(view);
  15. worldFrame.add(buttonPanel);
  16. worldFrame.validate();
  17. worldFrame.pack();
  18. worldFrame.setVisible(true);
  19.  
  20.  
  21. Scanner input = new Scanner(System.in);
  22. System.out.println("Welcome to Connect Four. The object of the game is to get four of the same colored pieces in a row.");
  23. System.out.println("Red will go first.");
  24.  
  25. //sets up the gameboard and then draws it
  26. for (int i = 0; i < connectFour.getGameBoard().length; i++) {
  27. for (int j = 0; j < connectFour.getGameBoard()[0].length; j++) {
  28. connectFour.getGameBoard()[i][j] = 0;
  29. }
  30. }
  31.  
  32. view.repaint();
  33.  
  34. connectFour.printGameBoard();
  35.  
  36. String pwon = "";
  37. boolean playgame=true;
  38. //actual gamecode
  39. while(playgame=true){
  40. boolean game=false;
  41. while (game == false) {
  42.  
  43. System.out.println("Red, which column would you like to move into?");
  44. System.out.println("Note Column Numbers 1 to 7 are accepted.");
  45.  
  46. int x = input.nextInt();
  47. connectFour.move1(x, connectFour.getGameBoard());
  48. if ((connectFour.getGameBoard()[0][x-1]==1)||(connectFour.getGameBoard()[0][x-1]==2)){
  49. System.out.println("Invalid move. You lost your turn");
  50. }
  51. view.repaint();
  52. connectFour.printGameBoard();
  53.  
  54. if (connectFour.isFourInaRow()) {
  55. pwon = "Red";
  56. break;
  57. }
  58.  
  59. System.out.println("Black, which column would you like to move into?");
  60. System.out.println("Note Column Numbers 1 to 7 are accepted.");
  61.  
  62. x = input.nextInt();
  63. connectFour.move2(x, connectFour.getGameBoard());
  64. if ((connectFour.getGameBoard()[0][x-1]==1)||(connectFour.getGameBoard()[0][x-1]==2)){
  65. System.out.println("Invalid move. You lost your turn");
  66. }
  67. view.repaint();
  68. connectFour.printGameBoard();
  69.  
  70. if (connectFour.isFourInaRow()) {
  71. pwon = "Black";
  72. break;
  73. }
  74.  
  75. }
  76.  
  77. if (connectFour.checkRow() == true) {
  78. System.out.println("row");
  79. } else if (connectFour.checkColumn() == true) {
  80. System.out.println("col");
  81. } else if (connectFour.checkDiag() == true) {
  82. System.out.println("diag");
  83. } else{
  84. System.out.println("draw");
  85.  
  86. }
  87.  
  88. System.out.println("Congrats " + pwon + " won the game.");
  89. System.out.println("Would you like to play again? Y/N");
  90. input.nextLine();
  91. String answer = input.nextLine();
  92.  
  93. if((answer.equals("N"))||(answer.equals("n"))){
  94. break;
  95. }
  96. }
  97. System.out.println("Thanks for playing.");
  98.  
  99.  
  100. }
  101.  
  102. input.nextLine();
  103. String answer = input.nextLine();
  104.  
  105. String answer = input.nextLine();
  106.  
  107. if ( answer . toUpperCase ( ) . equals ( "N" ) )
  108. {
  109. System . out . println ( "Thanks for playing!" ) ;
  110. System . exit ( 0 ) ;
  111. }
Add Comment
Please, Sign In to add comment