473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Connect 4 Problems

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?
Nov 9 '08 #1
3 1824
JosAH
11,448 Expert 8TB
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.
Nov 9 '08 #2
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?
Nov 9 '08 #3
JosAH
11,448 Expert 8TB
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
Nov 9 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Marc | last post by:
Hello, Newbie here..... Searching and working this for a week now. We too are having the same problems. Using MySql 4.0.14 and there are "no problems" at all.
2
by: John Glista | last post by:
Hello. I'm having a bit of a problem with the HTTPSConnection class. It is part of the httplib module. I discovered that it is not neccessary to specifiy a key and a certificate when opening an...
1
by: Ryan | last post by:
I have two similar stored procedures which I'm running. One runs and one doesn't. I can run both with no problems in SQL Enterprise (7.0 standard) and have checked the permissions and am happy with...
0
by: CristianMori | last post by:
Hi all I find out that if I try to connect a socket in the .net compact framework to a port that has no service binded to it, the socket tell me that it is connected anyway!!! I would like to...
2
by: Tim V. | last post by:
Here's the layout: AIX v5.2, DB2 v8 fp8 running in 64bit I've got a Multi-partitioned db running on lpar4 and I want to connect it to 2 instances running on lpar13. We'll deal with just 1...
5
by: mayamorning123 | last post by:
A comparison among six VSS remote tools including SourceOffSite , SourceAnyWhere, VSS Connect, SourceXT, VSS Remoting, VSS.NET To view the full article, please visit...
7
by: Jerome Lyles | last post by:
I'm trying to build a database. But none of the users I try to use works. I as normal user doesn't work: :~> createdb mydb createdb: could not connect to database template1: FATAL: user...
0
by: ZR | last post by:
I am writing two applications which needs to (among other things) communicate through network, so one of them is a client and the other one is a server. I have used asynchronous socket examples...
2
by: daft | last post by:
Hi guys Following on from an issue a couple of years back, now archived: http://www.thescripts.com/forum/thread208561-tabledef.connect.html I'm having the same problems. I can update the...
7
by: koonda | last post by:
Hi all, I posted my message earliar and I got some positive feedbacks but that didn't help me to solve some programming problems. I have an Assignment due 20th of this month, next monday. The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.