fork download
  1. //Stephen Ai
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class CoinStrip{
  6.  
  7. private boolean[] strip;
  8. private int turn;
  9. public int sizeStrip;
  10. public int numCoins;
  11.  
  12. public CoinStrip(){
  13. sizeStrip = getSize();
  14. numCoins = getCoins();
  15. strip = new boolean[sizeStrip];
  16. }
  17.  
  18. //Sets the board to all empty values - Clears strip.
  19. public void makeStrip(){
  20. for (int i = 0; i < sizeStrip; i++){
  21. strip[i] = false;
  22. }
  23. }
  24.  
  25. //Reads user input for size.
  26. public int getSize(){
  27. int size;
  28. Scanner s = new Scanner();
  29. System.out.println("Enter board size: ");
  30. size = s.nextInt();
  31. return size;
  32. }
  33.  
  34. //Reads use input for coins.
  35. public int getCoins(){
  36. int coins;
  37. Scanner s = new Scanner();
  38. System.out.println("Enter number of coins: ");
  39. coins = s.nextInt();
  40. return coins;
  41. }
  42.  
  43. //Checks if there is a coin at a given position.
  44. public boolean isCoin (int position){
  45. return strip[postion];
  46. }
  47.  
  48. //Adds coin to strip at a given position.
  49. public void addCoin (int position){
  50. strip[position] = true;
  51. }
  52.  
  53. //Removes coin from strip at a given position.
  54. public void removeCoin (int position){
  55. strip[position] = false;
  56. }
  57.  
  58. //Sets up strip for play.
  59. public void GameSetup(){
  60. int position;
  61. Random r = new Random();
  62. for (int i = 0; i < numCoins; i++){
  63. position = r.nextInt(sizeStrip);
  64. if (isCoin(setup) == false){
  65. addCoin(setup);
  66. }else{
  67. i--;
  68. }
  69. }
  70. }
  71.  
  72. //Checks spaces from old to move for coins.
  73. public boolean isLegal (int old, int move){
  74. boolean canMove = true;
  75. for (int i = move; i < old; i++){
  76. if (IsCoin(i) == true){
  77. canMove = false;
  78. break;
  79. }
  80. }
  81. return canmove;
  82. }
  83.  
  84. //Checks for move validity and moves coin.
  85. public void moveCoin (int old, int move){
  86. if (isLegal(old, move) == false){
  87. removeCoin(old);
  88. addCoin(move);
  89. }
  90. }
  91.  
  92. //Checks first number of spaces equal to amount of coins for coins.
  93. //Determines if game is over.
  94. public boolean isGameOver(){
  95. gameEnd = true;
  96. for (int i = 0; i < numCoins; i++){
  97. if (isCoin(i) == false){
  98. gameEnd = false;
  99. break;
  100. }
  101. }
  102. return gameEnd;
  103. }
  104.  
  105. public void getWinner(){
  106. if (turn % 2 == 1){
  107. System.out.println("Player 2 Wins!");
  108. }
  109. if (turn % 2 == 0){
  110. System.out.println("Player 1 Wins!");
  111. }
  112. }
  113.  
  114. //Generates representation of the board
  115. public String ToString(){
  116. String board = "";
  117. board = board + "--------------------/n";
  118. for (int i = 0; i < numCoins; i++){
  119. board = board + " " + i;
  120. }
  121. board = board + "/n";
  122. for (int i = 0; i < numCoins; i++){
  123. if (IsCoin(i) == false){
  124. board = board + "| ";
  125. }
  126. if (IsCoin(i) == true){
  127. board = board + "|O";
  128. }
  129. }
  130. board = board + "|/n";
  131. board = board + "--------------------/n";
  132. return board;
  133. }
  134.  
  135. public void playTurn(){
  136. Scanner sc = new Scanner(System.in);
  137. turn = 0;
  138. System.out.println(ToString());
  139. while(IsGameOver() == false){
  140. System.out.println("Enter coin to move: ");
  141. before = sc.nextInt();
  142. while(IsCoin(before) == false){
  143. System.out.println("Invalid! Coin doesn't exist!");
  144. System.out.println("Enter coin to move: ");
  145. before = sc.nextInt();
  146. }
  147. System.out.println("Enter space to move to: ");
  148. after = sc.nextInt();
  149. while(IsCoin(after) == true && IsLegal(before, after) == false){
  150. System.out.println("Invalid! Please choose a valid move.");
  151. System.out.println("Enter space to move to: ");
  152. after = sc.nextInt();
  153. }
  154. MoveCoin(before, after);
  155. turn++;
  156. }
  157. getWinner();
  158. }
  159.  
  160. public static void main (String args[]){
  161. CoinStrip game = new CoinStrip();
  162. game.gameSetup();
  163. game.playTurn();
  164. }
  165. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:5: error: class CoinStrip is public, should be declared in a file named CoinStrip.java
public class CoinStrip{
       ^
Main.java:28: error: no suitable constructor found for Scanner(no arguments)
        Scanner s = new Scanner();
                    ^
    constructor Scanner.Scanner(Readable,Pattern) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(Readable) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(InputStream) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(InputStream,String) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(File) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(File,String) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(File,CharsetDecoder) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(Path) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(Path,String) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(Path,Charset) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(String) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(ReadableByteChannel) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(ReadableByteChannel,String) is not applicable
      (actual and formal argument lists differ in length)
Main.java:37: error: no suitable constructor found for Scanner(no arguments)
        Scanner s = new Scanner();
                    ^
    constructor Scanner.Scanner(Readable,Pattern) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(Readable) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(InputStream) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(InputStream,String) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(File) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(File,String) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(File,CharsetDecoder) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(Path) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(Path,String) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(Path,Charset) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(String) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(ReadableByteChannel) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(ReadableByteChannel,String) is not applicable
      (actual and formal argument lists differ in length)
Main.java:45: error: cannot find symbol
        return strip[postion];
                     ^
  symbol:   variable postion
  location: class CoinStrip
Main.java:64: error: cannot find symbol
            if (isCoin(setup) == false){
                       ^
  symbol:   variable setup
  location: class CoinStrip
Main.java:65: error: cannot find symbol
                addCoin(setup);
                        ^
  symbol:   variable setup
  location: class CoinStrip
Main.java:76: error: cannot find symbol
            if (IsCoin(i) == true){
                ^
  symbol:   method IsCoin(int)
  location: class CoinStrip
Main.java:81: error: cannot find symbol
        return canmove;
               ^
  symbol:   variable canmove
  location: class CoinStrip
Main.java:95: error: cannot find symbol
        gameEnd = true;
        ^
  symbol:   variable gameEnd
  location: class CoinStrip
Main.java:98: error: cannot find symbol
                gameEnd = false;
                ^
  symbol:   variable gameEnd
  location: class CoinStrip
Main.java:102: error: cannot find symbol
        return gameEnd;
               ^
  symbol:   variable gameEnd
  location: class CoinStrip
Main.java:123: error: cannot find symbol
            if (IsCoin(i) == false){
                ^
  symbol:   method IsCoin(int)
  location: class CoinStrip
Main.java:126: error: cannot find symbol
            if (IsCoin(i) == true){
                ^
  symbol:   method IsCoin(int)
  location: class CoinStrip
Main.java:139: error: cannot find symbol
        while(IsGameOver() == false){
              ^
  symbol:   method IsGameOver()
  location: class CoinStrip
Main.java:141: error: cannot find symbol
           before = sc.nextInt();
           ^
  symbol:   variable before
  location: class CoinStrip
Main.java:142: error: cannot find symbol
           while(IsCoin(before) == false){
                        ^
  symbol:   variable before
  location: class CoinStrip
Main.java:145: error: cannot find symbol
               before = sc.nextInt();
               ^
  symbol:   variable before
  location: class CoinStrip
Main.java:148: error: cannot find symbol
           after = sc.nextInt();
           ^
  symbol:   variable after
  location: class CoinStrip
Main.java:149: error: cannot find symbol
           while(IsCoin(after) == true && IsLegal(before, after) == false){
                        ^
  symbol:   variable after
  location: class CoinStrip
Main.java:149: error: cannot find symbol
           while(IsCoin(after) == true && IsLegal(before, after) == false){
                                                  ^
  symbol:   variable before
  location: class CoinStrip
Main.java:149: error: cannot find symbol
           while(IsCoin(after) == true && IsLegal(before, after) == false){
                                                          ^
  symbol:   variable after
  location: class CoinStrip
Main.java:152: error: cannot find symbol
               after = sc.nextInt();
               ^
  symbol:   variable after
  location: class CoinStrip
Main.java:154: error: cannot find symbol
           MoveCoin(before, after);
                    ^
  symbol:   variable before
  location: class CoinStrip
Main.java:154: error: cannot find symbol
           MoveCoin(before, after);
                            ^
  symbol:   variable after
  location: class CoinStrip
Main.java:162: error: cannot find symbol
        game.gameSetup();
            ^
  symbol:   method gameSetup()
  location: variable game of type CoinStrip
25 errors
stdout
Standard output is empty