Connect 4 Problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KTsquare
    New Member
    • Nov 2008
    • 3

    Connect 4 Problems

    Code:
    import java.util.Scanner;
    public class assignment{
    public static void main(String args[]){
    String getError;
    int result;
    int f,s,q,em=0;
    int c0=0,c1=0,c2=0,c3=0,c4=0,c5=0,c6=0;
    int drawBoard[][]=new int[6][7];
    
    while (em==0){
    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]+
    		 "\n4 | "+drawBoard[4][0]+" "+drawBoard[4][1]+" "+drawBoard[4][2]+" "+drawBoard[4][3]+" "+drawBoard[4][4]+" "+drawBoard[4][5]+" "+drawBoard[4][6]+
    		 "\n3 | "+drawBoard[3][0]+" "+drawBoard[3][1]+" "+drawBoard[3][2]+" "+drawBoard[3][3]+" "+drawBoard[3][4]+" "+drawBoard[3][5]+" "+drawBoard[3][6]+
    		 "\n2 | "+drawBoard[2][0]+" "+drawBoard[2][1]+" "+drawBoard[2][2]+" "+drawBoard[2][3]+" "+drawBoard[2][4]+" "+drawBoard[2][5]+" "+drawBoard[2][6]+
    		 "\n1 | "+drawBoard[1][0]+" "+drawBoard[1][1]+" "+drawBoard[1][2]+" "+drawBoard[1][3]+" "+drawBoard[1][4]+" "+drawBoard[1][5]+" "+drawBoard[1][6]+
    		 "\n0 | "+drawBoard[0][0]+" "+drawBoard[0][1]+" "+drawBoard[0][2]+" "+drawBoard[0][3]+" "+drawBoard[0][4]+" "+drawBoard[0][5]+" "+drawBoard[0][6]+
    		 "\n    -------------\n    0 1 2 3 4 5 6");
    
    Scanner keyin=new Scanner(System.in);
    System.out.print("Player 1 type a column (0-6) or 9 to quit current game:");
    
    f=keyin.nextInt();
    switch(f){
    case 0:
    drawBoard[c0][0]=1;
    c0++;
    break;
    case 1:
    drawBoard[c1][1]=1;
    c1++;
    break;
    case 2: 
    drawBoard[c2][2]=1;
    c2++;
    break;
    case 3:
    drawBoard[c3][3]=1;
    c3++;
    break;
    case 4:
    drawBoard[c4][4]=1;
    c4++;
    break;
    case 5:
    drawBoard[c5][5]=1;
    c5++;
    break;
    case 6:
    drawBoard[c6][6]=1;
    c6++;
    break;
    case 9:
    System.out.print("Do you want to continue (Yes=1, No=0)?");
    q=keyin.nextInt();
    	
    
    if (q==1)
    continue;
    else if (q==0)
    System.exit(0);
    }
    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]+
    		 "\n4 | "+drawBoard[4][0]+" "+drawBoard[4][1]+" "+drawBoard[4][2]+" "+drawBoard[4][3]+" "+drawBoard[4][4]+" "+drawBoard[4][5]+" "+drawBoard[4][6]+
    		 "\n3 | "+drawBoard[3][0]+" "+drawBoard[3][1]+" "+drawBoard[3][2]+" "+drawBoard[3][3]+" "+drawBoard[3][4]+" "+drawBoard[3][5]+" "+drawBoard[3][6]+
    		 "\n2 | "+drawBoard[2][0]+" "+drawBoard[2][1]+" "+drawBoard[2][2]+" "+drawBoard[2][3]+" "+drawBoard[2][4]+" "+drawBoard[2][5]+" "+drawBoard[2][6]+
    		 "\n1 | "+drawBoard[1][0]+" "+drawBoard[1][1]+" "+drawBoard[1][2]+" "+drawBoard[1][3]+" "+drawBoard[1][4]+" "+drawBoard[1][5]+" "+drawBoard[1][6]+
    		 "\n0 | "+drawBoard[0][0]+" "+drawBoard[0][1]+" "+drawBoard[0][2]+" "+drawBoard[0][3]+" "+drawBoard[0][4]+" "+drawBoard[0][5]+" "+drawBoard[0][6]+
    		 "\n    -------------\n    0 1 2 3 4 5 6");
    System.out.print("Player 2 type a column (0-6) or 9 to quit current game:");
    s=keyin.nextInt();
    switch(s){
    case 0:
    drawBoard[c0][0]=2;
    c0++;
    break;
    case 1:
    drawBoard[c1][1]=2;
    c1++;
    break;
    case 2:
    drawBoard[c2][2]=2;
    c2++;
    break;
    case 3:
    drawBoard[c3][3]=2;
    c3++;
    break;
    case 4:
    drawBoard[c4][4]=2;
    c4++;
    break;
    case 5:
    drawBoard[c5][5]=2;
    c5++;
    break;
    case 6:
    drawBoard[c6][6]=2;
    c6++;
    break;
    case 9:
    System.out.print("Do you want to continue (Yes=1, No=0)?");
    q=keyin.nextInt();
    if (q==1)
    continue;
    else if (q==0)
    System.exit(0);
    }
    
    for(int i=1; i<=4; i++){
     result = status(i);
    }
    }
    
    public static int hasWon(){
    int status = 0;
    
    
    for (int row=0; row<6; row++)
    {
    for (int col=0; col<4; col++)
    {
    if (ConnectFourArray[col][row] != 0 &&
    ConnectFourArray[col][row] == ConnectFourArray[col+1][row] &&
    ConnectFourArray[col][row] == ConnectFourArray[col+2][row] &&
    ConnectFourArray[col][row] == ConnectFourArray[col+3][row])
    {status = 1;}
    //status = true;//int winner;
    
    if(status == 1)
    {
    
    
    System.out.println("Player 1 is the winner");
    }
    
    else if(status == 0)
    {
    
    
    System.out.println("Player 2 is the winner" );
    }
    
    }//end inner for loop
    }// end outer for loop
    } // end method Winner
    
    return status;
    Why i can't open the new main of this cheak win?
    Last edited by JosAH; Nov 9 '08, 08:17 AM. Reason: added [code] ... [/code] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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.

    Comment

    • KTsquare
      New Member
      • Nov 2008
      • 3

      #3
      [code=java]//Program to do Connect 4.
      //by Neil Broadbent

      import java.io.Buffere dReader;
      import java.io.InputSt reamReader;
      import java.io.IOExcep tion;
      import java.util.*;
      import NeilClass.*;


      public class k
      {
      final static int GRID_WIDTH=7;
      final static int GRID_HEIGHT=5;

      public static boolean GAME_OVER=false ;
      public static boolean NoughtGo = true;
      public static String tTeam = "Nought";
      public static int[][] aGrid = new int[GRID_WIDTH][GRID_HEIGHT];
      public static boolean COMPUTER_GO = false;
      public static int TURN_NUMBER=0;

      public static int X=0; //The actual X co-ordinate on the board (1-7)
      public static int Y=0; //The actual Y co-ordinate on the board
      public static boolean VALID=false;

      public static void main(String[] pArgs) throws IOException
      {
      for (int rows=0; rows < GRID_HEIGHT; rows++){
      for (int columns=0; columns < GRID_WIDTH; columns++){
      aGrid[columns][rows] = 0;
      }
      }
      printgrid(aGrid );


      while (!GAME_OVER){

      VALID=false;
      GetMove();

      if (Y==-1){
      GAME_OVER=true;
      System.exit(1);
      }

      if (FindY(X-1)) {
      System.out.prin tln("Find X " + FindY(X-1) + " " + X);


      if (NoughtGo){
      aGrid[X-1][Y-1]=-1;
      NoughtGo=false;
      tTeam = "Cross";
      }
      else{
      aGrid[X-1][Y-1]=1;
      NoughtGo=true;
      tTeam="Nought";

      }
      printgrid(aGrid );
      checkmove(aGrid );

      TURN_NUMBER++;
      if (TURN_NUMBER == (GRID_WIDTH*GRI D_HEIGHT)){
      System.out.prin tln("Game Over - It's a Draw");
      System.exit(1);
      }

      }else{
      System.out.prin tln("Illegal move - try again " + X + "," + Y);
      }

      }

      if(GAME_OVER){
      GameOverProc();
      }
      }


      public static void printgrid(int[][] aGrid)
      {
      for (int rows=GRID_HEIGH T-1; rows>=0; rows--){
      System.out.prin t("* ");
      for (int columns=0; columns<GRID_WI DTH; columns++){
      if ((aGrid[columns][rows]) == -1)
      {
      System.out.prin t(" 0");
      }
      else if((aGrid[columns][rows]) == 1)
      {
      System.out.prin t(" X");
      }
      else{
      System.out.prin t(" -");
      }
      }
      System.out.prin tln();
      }
      System.out.prin tln(" 1 2 3 4 5 6 7");
      }


      public static void checkmove(int[][] aGrid)
      {
      //Check Rows
      for (int rows=0; rows < GRID_HEIGHT; rows++){
      for (int columns=0; columns < GRID_WIDTH-3; columns++){
      int tCheck = (aGrid[columns][rows]) + (aGrid[columns+1][rows]) + (aGrid[columns+2][rows]) + (aGrid[columns+3][rows]);
      if (tCheck == -4 || tCheck == 4)
      {
      System.out.prin tln("A Winner!");
      System.exit(1);
      }
      }
      }


      //Check Columns
      for (int rows=0; rows < GRID_HEIGHT-3; rows++){
      for (int columns=0; columns < GRID_WIDTH; columns++){
      int tCheck = (aGrid[columns][rows]) + (aGrid[columns][rows+1]) + (aGrid[columns][rows+2]) + (aGrid[columns][rows+3]);
      if (tCheck == -4 || tCheck == 4)
      {
      System.out.prin tln("A Winner!");
      System.exit(1);
      }
      }
      }


      //Check Diagonals (SW-NE)
      for (int rows=0; rows < GRID_HEIGHT-3; rows++){
      for (int columns=0; columns < GRID_WIDTH-3; columns++){
      int tCheck = (aGrid[columns][rows]) + (aGrid[columns+1][rows+1]) + (aGrid[columns+2][rows+2]) + (aGrid[columns+3][rows+3]);
      if (tCheck == -4 || tCheck == 4)
      {
      System.out.prin tln("A Winner!");
      System.exit(1);
      }
      }
      }


      //Check Diagonals (NW-SE)
      for (int rows=0; rows < GRID_HEIGHT-3; rows++){
      for (int columns=0; columns < GRID_WIDTH-3; columns++){
      int tCheck = (aGrid[columns][rows]) + (aGrid[columns+1][rows+1]) + (aGrid[columns+2][rows+2]) + (aGrid[columns+3][rows+3]);
      if (tCheck == -4 || tCheck == 4)
      {
      System.out.prin tln("A Winner!");
      System.exit(1);
      }
      }
      }


      //Check Diagonals (NE-SW)
      for (int rows=0; rows < GRID_HEIGHT-3; rows++){
      for (int columns=3; columns < GRID_WIDTH; columns++){
      int tCheck = (aGrid[columns][rows]) + (aGrid[columns-1][rows+1]) + (aGrid[columns-2][rows+2]) + (aGrid[columns-3][rows+3]);
      if (tCheck == -4 || tCheck == 4)
      {
      System.out.prin tln("A Winner!");
      System.exit(1);
      }
      }
      }
      }



      public static void GameOverProc()
      {
      System.out.prin tln("Game Over " + tTeam + " Won!");
      }


      //I never got time to add the code for the computer to make a move
      public static void ComputerMove()
      {

      for (int rows=0; rows<3; rows++){
      for (int columns=0; columns<3; columns++){
      }
      }
      }


      public static void GetMove() throws IOException
      {
      while (!VALID){
      try{
      String tInput = NeilClass.GetIn putString("Wher e do you want to put your " + tTeam + " eg(2)");
      final BufferedReader tKeyboard = new BufferedReader( new InputStreamRead er(System.in));
      StringTokenizer tTokensOnLine = new StringTokenizer (tInput, ",");

      String tThisToken = tTokensOnLine.n extToken();
      X = new Integer(tThisTo ken).intValue() ;

      if (X<=GRID_WIDTH || X==-1){
      VALID=true;
      }
      else{
      GetMove();
      }

      }
      catch(NumberFor matException e){
      System.out.prin tln("Invalid Co-ordinate");

      }
      catch(NoSuchEle mentException e){
      System.out.prin tln("Invalid Co-ordinate");
      }
      }
      }


      public static boolean FindY(int X)
      {
      boolean FOUND=false;
      boolean RETURN_VALUE=fa lse;
      int rows=0;

      while (!FOUND){
      if (rows==GRID_HEI GHT)
      {
      RETURN_VALUE=fa lse;
      FOUND=true;
      }
      else{
      if ((aGrid[X][rows]) == 0)
      {
      System.out.prin tln(rows);
      Y = rows+1;
      FOUND=true;
      RETURN_VALUE=tr ue;
      }
      rows++;
      }
      }
      return RETURN_VALUE;
      }
      }

      [/code]
      For this, plx tell me why the NeilClass does not exit?
      Last edited by Nepomuk; Nov 10 '08, 10:37 PM. Reason: Please use [CODE] tags

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        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

        Comment

        Working...