Connecting Tech Pros Worldwide Forums | Help | Site Map

Connect 4 Problems

Newbie
 
Join Date: Nov 2008
Posts: 2
#1: Nov 9 '08
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class assignment{
  3. public static void main(String args[]){
  4. String getError;
  5. int result;
  6. int f,s,q,em=0;
  7. int c0=0,c1=0,c2=0,c3=0,c4=0,c5=0,c6=0;
  8. int drawBoard[][]=new int[6][7];
  9.  
  10. while (em==0){
  11. System.out.println("5 | "+drawBoard[5][0]+" "+drawBoard[5][1]+" "+drawBoard[5][2]+" "+drawBoard[5][3]+" "+drawBoard[5][4]+" "+drawBoard[5][5]+" "+drawBoard[5][6]+
  12.          "\n4 | "+drawBoard[4][0]+" "+drawBoard[4][1]+" "+drawBoard[4][2]+" "+drawBoard[4][3]+" "+drawBoard[4][4]+" "+drawBoard[4][5]+" "+drawBoard[4][6]+
  13.          "\n3 | "+drawBoard[3][0]+" "+drawBoard[3][1]+" "+drawBoard[3][2]+" "+drawBoard[3][3]+" "+drawBoard[3][4]+" "+drawBoard[3][5]+" "+drawBoard[3][6]+
  14.          "\n2 | "+drawBoard[2][0]+" "+drawBoard[2][1]+" "+drawBoard[2][2]+" "+drawBoard[2][3]+" "+drawBoard[2][4]+" "+drawBoard[2][5]+" "+drawBoard[2][6]+
  15.          "\n1 | "+drawBoard[1][0]+" "+drawBoard[1][1]+" "+drawBoard[1][2]+" "+drawBoard[1][3]+" "+drawBoard[1][4]+" "+drawBoard[1][5]+" "+drawBoard[1][6]+
  16.          "\n0 | "+drawBoard[0][0]+" "+drawBoard[0][1]+" "+drawBoard[0][2]+" "+drawBoard[0][3]+" "+drawBoard[0][4]+" "+drawBoard[0][5]+" "+drawBoard[0][6]+
  17.          "\n    -------------\n    0 1 2 3 4 5 6");
  18.  
  19. Scanner keyin=new Scanner(System.in);
  20. System.out.print("Player 1 type a column (0-6) or 9 to quit current game:");
  21.  
  22. f=keyin.nextInt();
  23. switch(f){
  24. case 0:
  25. drawBoard[c0][0]=1;
  26. c0++;
  27. break;
  28. case 1:
  29. drawBoard[c1][1]=1;
  30. c1++;
  31. break;
  32. case 2: 
  33. drawBoard[c2][2]=1;
  34. c2++;
  35. break;
  36. case 3:
  37. drawBoard[c3][3]=1;
  38. c3++;
  39. break;
  40. case 4:
  41. drawBoard[c4][4]=1;
  42. c4++;
  43. break;
  44. case 5:
  45. drawBoard[c5][5]=1;
  46. c5++;
  47. break;
  48. case 6:
  49. drawBoard[c6][6]=1;
  50. c6++;
  51. break;
  52. case 9:
  53. System.out.print("Do you want to continue (Yes=1, No=0)?");
  54. q=keyin.nextInt();
  55.  
  56.  
  57. if (q==1)
  58. continue;
  59. else if (q==0)
  60. System.exit(0);
  61. }
  62. System.out.println("5 | "+drawBoard[5][0]+" "+drawBoard[5][1]+" "+drawBoard[5][2]+" "+drawBoard[5][3]+" "+drawBoard[5][4]+" "+drawBoard[5][5]+" "+drawBoard[5][6]+
  63.          "\n4 | "+drawBoard[4][0]+" "+drawBoard[4][1]+" "+drawBoard[4][2]+" "+drawBoard[4][3]+" "+drawBoard[4][4]+" "+drawBoard[4][5]+" "+drawBoard[4][6]+
  64.          "\n3 | "+drawBoard[3][0]+" "+drawBoard[3][1]+" "+drawBoard[3][2]+" "+drawBoard[3][3]+" "+drawBoard[3][4]+" "+drawBoard[3][5]+" "+drawBoard[3][6]+
  65.          "\n2 | "+drawBoard[2][0]+" "+drawBoard[2][1]+" "+drawBoard[2][2]+" "+drawBoard[2][3]+" "+drawBoard[2][4]+" "+drawBoard[2][5]+" "+drawBoard[2][6]+
  66.          "\n1 | "+drawBoard[1][0]+" "+drawBoard[1][1]+" "+drawBoard[1][2]+" "+drawBoard[1][3]+" "+drawBoard[1][4]+" "+drawBoard[1][5]+" "+drawBoard[1][6]+
  67.          "\n0 | "+drawBoard[0][0]+" "+drawBoard[0][1]+" "+drawBoard[0][2]+" "+drawBoard[0][3]+" "+drawBoard[0][4]+" "+drawBoard[0][5]+" "+drawBoard[0][6]+
  68.          "\n    -------------\n    0 1 2 3 4 5 6");
  69. System.out.print("Player 2 type a column (0-6) or 9 to quit current game:");
  70. s=keyin.nextInt();
  71. switch(s){
  72. case 0:
  73. drawBoard[c0][0]=2;
  74. c0++;
  75. break;
  76. case 1:
  77. drawBoard[c1][1]=2;
  78. c1++;
  79. break;
  80. case 2:
  81. drawBoard[c2][2]=2;
  82. c2++;
  83. break;
  84. case 3:
  85. drawBoard[c3][3]=2;
  86. c3++;
  87. break;
  88. case 4:
  89. drawBoard[c4][4]=2;
  90. c4++;
  91. break;
  92. case 5:
  93. drawBoard[c5][5]=2;
  94. c5++;
  95. break;
  96. case 6:
  97. drawBoard[c6][6]=2;
  98. c6++;
  99. break;
  100. case 9:
  101. System.out.print("Do you want to continue (Yes=1, No=0)?");
  102. q=keyin.nextInt();
  103. if (q==1)
  104. continue;
  105. else if (q==0)
  106. System.exit(0);
  107. }
  108.  
  109. for(int i=1; i<=4; i++){
  110.  result = status(i);
  111. }
  112. }
  113.  
  114. public static int hasWon(){
  115. int status = 0;
  116.  
  117.  
  118. for (int row=0; row<6; row++)
  119. {
  120. for (int col=0; col<4; col++)
  121. {
  122. if (ConnectFourArray[col][row] != 0 &&
  123. ConnectFourArray[col][row] == ConnectFourArray[col+1][row] &&
  124. ConnectFourArray[col][row] == ConnectFourArray[col+2][row] &&
  125. ConnectFourArray[col][row] == ConnectFourArray[col+3][row])
  126. {status = 1;}
  127. //status = true;//int winner;
  128.  
  129. if(status == 1)
  130. {
  131.  
  132.  
  133. System.out.println("Player 1 is the winner");
  134. }
  135.  
  136. else if(status == 0)
  137. {
  138.  
  139.  
  140. System.out.println("Player 2 is the winner" );
  141. }
  142.  
  143. }//end inner for loop
  144. }// end outer for loop
  145. } // end method Winner
  146.  
  147. return status;
  148.  
Why i can't open the new main of this cheak win?

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Nov 9 '08

re: Connect 4 Problems


Quote:

Originally Posted by KTsquare

Why i can't open the new main of this cheak win?

I don't understand your question, please rephrase it. btw, your code is overly
repetative and not very well organized.

kind regards,

Jos

ps. I added those [ code ] ... [ /code ] tags for you.
Newbie
 
Join Date: Nov 2008
Posts: 2
#3: Nov 9 '08

re: Connect 4 Problems


Expand|Select|Wrap|Line Numbers
  1. //Program to do Connect 4.
  2. //by Neil Broadbent
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6. import java.io.IOException;
  7. import java.util.*;
  8. import NeilClass.*;
  9.  
  10.  
  11. public class k
  12. {
  13.     final static int GRID_WIDTH=7;
  14.     final static int GRID_HEIGHT=5;
  15.  
  16.     public static boolean GAME_OVER=false;
  17.     public static boolean NoughtGo = true;
  18.     public static String tTeam = "Nought";
  19.     public static int[][] aGrid = new int[GRID_WIDTH][GRID_HEIGHT];
  20.     public static boolean COMPUTER_GO = false;
  21.     public static int TURN_NUMBER=0;
  22.  
  23.     public static int X=0; //The actual X co-ordinate on the board (1-7)
  24.     public static int Y=0; //The actual Y co-ordinate on the board
  25.     public static boolean VALID=false;
  26.  
  27.     public static void main(String[] pArgs) throws IOException
  28.         {
  29.             for (int rows=0; rows < GRID_HEIGHT; rows++){
  30.                 for (int columns=0; columns < GRID_WIDTH; columns++){
  31.                     aGrid[columns][rows] = 0;
  32.                 }
  33.             }
  34.             printgrid(aGrid);
  35.  
  36.  
  37.        while (!GAME_OVER){
  38.  
  39.        VALID=false;
  40.        GetMove();
  41.  
  42.            if (Y==-1){
  43.                GAME_OVER=true;
  44.                System.exit(1);
  45.            }
  46.  
  47.            if (FindY(X-1)) {
  48.                System.out.println("Find X " + FindY(X-1) + " " + X);
  49.  
  50.  
  51.                if (NoughtGo){
  52.                    aGrid[X-1][Y-1]=-1;
  53.                    NoughtGo=false;
  54.                    tTeam = "Cross";
  55.                }
  56.                else{
  57.                    aGrid[X-1][Y-1]=1;
  58.                    NoughtGo=true;
  59.                    tTeam="Nought";
  60.  
  61.                }
  62.                printgrid(aGrid);
  63.                checkmove(aGrid);
  64.  
  65.                TURN_NUMBER++;
  66.                if (TURN_NUMBER == (GRID_WIDTH*GRID_HEIGHT)){
  67.                    System.out.println("Game Over - It's a Draw");
  68.                    System.exit(1);
  69.                }
  70.  
  71.            }else{
  72.                System.out.println("Illegal move - try again " + X + "," + Y);
  73.            }
  74.  
  75.         }
  76.  
  77.          if(GAME_OVER){
  78.              GameOverProc();
  79.          }
  80.         }
  81.  
  82.  
  83.     public static void printgrid(int[][] aGrid)
  84.         {
  85.             for (int rows=GRID_HEIGHT-1; rows>=0; rows--){
  86.                 System.out.print("* ");
  87.                 for (int columns=0; columns<GRID_WIDTH; columns++){
  88.                     if ((aGrid[columns][rows]) == -1)
  89.                         {
  90.                             System.out.print(" 0");
  91.                         }
  92.                     else if((aGrid[columns][rows]) == 1)
  93.                         {
  94.                             System.out.print(" X");
  95.                         }
  96.                     else{
  97.                         System.out.print(" -");
  98.                     }
  99.                 }
  100.                 System.out.println();
  101.             }
  102.             System.out.println("   1 2 3 4 5 6 7");
  103.         }
  104.  
  105.  
  106.     public static void checkmove(int[][] aGrid)
  107.         {
  108.                 //Check Rows
  109.             for (int rows=0; rows < GRID_HEIGHT; rows++){
  110.                 for (int columns=0; columns < GRID_WIDTH-3; columns++){
  111.                     int tCheck = (aGrid[columns][rows]) + (aGrid[columns+1][rows]) + (aGrid[columns+2][rows]) + (aGrid[columns+3][rows]);
  112.                     if (tCheck == -4 || tCheck == 4)
  113.                     {
  114.                         System.out.println("A Winner!");
  115.                         System.exit(1);
  116.                     }
  117.                 }
  118.             }
  119.  
  120.  
  121.                 //Check Columns
  122.             for (int rows=0; rows < GRID_HEIGHT-3; rows++){
  123.                 for (int columns=0; columns < GRID_WIDTH; columns++){
  124.                     int tCheck = (aGrid[columns][rows]) + (aGrid[columns][rows+1]) + (aGrid[columns][rows+2]) + (aGrid[columns][rows+3]);
  125.                     if (tCheck == -4 || tCheck == 4)
  126.                     {
  127.                         System.out.println("A Winner!");
  128.                         System.exit(1);
  129.                     }
  130.                 }
  131.             }
  132.  
  133.  
  134.                 //Check Diagonals (SW-NE)
  135.             for (int rows=0; rows < GRID_HEIGHT-3; rows++){
  136.                 for (int columns=0; columns < GRID_WIDTH-3; columns++){
  137.                     int tCheck = (aGrid[columns][rows]) + (aGrid[columns+1][rows+1]) + (aGrid[columns+2][rows+2]) + (aGrid[columns+3][rows+3]);
  138.                     if (tCheck == -4 || tCheck == 4)
  139.                     {
  140.                         System.out.println("A Winner!");
  141.                         System.exit(1);
  142.                     }
  143.                 }
  144.             }
  145.  
  146.  
  147.                 //Check Diagonals (NW-SE)
  148.             for (int rows=0; rows < GRID_HEIGHT-3; rows++){
  149.                 for (int columns=0; columns < GRID_WIDTH-3; columns++){
  150.                     int tCheck = (aGrid[columns][rows]) + (aGrid[columns+1][rows+1]) + (aGrid[columns+2][rows+2]) + (aGrid[columns+3][rows+3]);
  151.                     if (tCheck == -4 || tCheck == 4)
  152.                     {
  153.                         System.out.println("A Winner!");
  154.                         System.exit(1);
  155.                     }
  156.                 }
  157.             }
  158.  
  159.  
  160.                 //Check Diagonals (NE-SW)
  161.             for (int rows=0; rows < GRID_HEIGHT-3; rows++){
  162.                 for (int columns=3; columns < GRID_WIDTH; columns++){
  163.                     int tCheck = (aGrid[columns][rows]) + (aGrid[columns-1][rows+1]) + (aGrid[columns-2][rows+2]) + (aGrid[columns-3][rows+3]);
  164.                     if (tCheck == -4 || tCheck == 4)
  165.                     {
  166.                         System.out.println("A Winner!");
  167.                         System.exit(1);
  168.                     }
  169.                 }
  170.             }
  171.         }
  172.  
  173.  
  174.  
  175.     public static void GameOverProc()
  176.         {
  177.             System.out.println("Game Over " + tTeam + " Won!");
  178.         }
  179.  
  180.  
  181.         //I never got time to add the code for the computer to make a move
  182.    public static void ComputerMove()
  183.         {
  184.  
  185.             for (int rows=0; rows<3; rows++){
  186.                 for (int columns=0; columns<3; columns++){
  187.                 }
  188.             }
  189.         }
  190.  
  191.  
  192.     public static void GetMove() throws IOException
  193.     {
  194.             while (!VALID){
  195.                    try{
  196.                        String tInput = NeilClass.GetInputString("Where do you want to put your " + tTeam + " eg(2)");
  197.                        final BufferedReader tKeyboard = new BufferedReader(new InputStreamReader(System.in));
  198.                        StringTokenizer tTokensOnLine = new StringTokenizer(tInput, ",");
  199.  
  200.                        String tThisToken = tTokensOnLine.nextToken();
  201.                        X = new Integer(tThisToken).intValue();
  202.  
  203.                        if (X<=GRID_WIDTH || X==-1){
  204.                            VALID=true;
  205.                        }
  206.                        else{
  207.                            GetMove();
  208.                        }
  209.  
  210.                    }
  211.                    catch(NumberFormatException e){
  212.                        System.out.println("Invalid Co-ordinate");
  213.  
  214.                    }
  215.                    catch(NoSuchElementException e){
  216.                        System.out.println("Invalid Co-ordinate");
  217.                    }
  218.                }
  219.     }
  220.  
  221.  
  222.     public static boolean FindY(int X)
  223.     {
  224.     boolean FOUND=false;
  225.     boolean RETURN_VALUE=false;
  226.         int rows=0;    
  227.  
  228.         while (!FOUND){
  229.                     if (rows==GRID_HEIGHT)
  230.                         {
  231.                             RETURN_VALUE=false;
  232.                             FOUND=true;
  233.                         }
  234.                     else{
  235.             if ((aGrid[X][rows]) == 0)
  236.             {
  237.                 System.out.println(rows);
  238.                 Y = rows+1;
  239.                                 FOUND=true;
  240.                                 RETURN_VALUE=true;
  241.             }
  242.                         rows++;
  243.                     }
  244.         }
  245.                 return RETURN_VALUE;
  246.     }
  247. }
  248.  
  249.  
For this, plx tell me why the NeilClass does not exit?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Nov 9 '08

re: Connect 4 Problems


Quote:

Originally Posted by KTsquare

For this, plx tell me why the NeilClass does not exit?

I don't know why that class doesn't exist. Maybe you haven't compiled it or
maybe it can't be found in the classpath list. Please don't dump all your code
here without those [ code ] tags. Read the instructions in the 'help' link before
you post here. (see the top of this page)

Also read the "Read This First" article near the top of this page for useful links.

kind regards,

Jos
Reply