-1

This code is not done yet, but I'm keep getting the problem where the txt file (cpu.txt) becomes "null" in the output, does anyone know where I did wrong? Thanks.

This is the output:

--------------------Configuration: --------------------

BINGO CALL IS: G50

PLAYER CARDS:
  B   I   N   G   O           B   I   N   G   O
1   21  38  58  68  
5   29  31  51  73  
15  20  F   59  67  
4   30  39  53  74  
11  28  33  48  69  
7   30  32  49  69  
8   27  35  58  63  
3   17  F   57  68  
11  18  45  52  71  
12  16  33  54  70  

CPU CARDS:
  B   I   N   G   O           B   I   N   G   O
null    null    null    null    null    
null    null    null    null    null    
null    null    null    null    null    
null    null    null    null    null    
null    null    null    null    null    
null    null    null    null    null    
null    null    null    null    null    
null    null    null    null    null    
null    null    null    null    null    
null    null    null    null    null    


B   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  
I   16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  
N   31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  
G   46  47  48  49  *   51  52  53  54  55  56  57  58  59  60  
O   61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  

current points available: 0 point. 1. Daub board 2. Call Bingo! 3. Use bonus (1000 points) 4. Next call

    import java.util.Random;
    import java.io.*;
    import java.util.Scanner;

    public class Bingo_D {

    public static void callBoard(int[] un, int turns) {


            int t = 1;
            int i = 1;

            int[] bingo = new int[76];
            System.out.print("B" + "\t");
            String star= "nothing";
            for ( i=1; i < bingo.length; i++){

                bingo[i] = i;

                for (t = 1; t < un.length; t++){
                if(un[t] == bingo[i]){ //un = call, bingo = board
                star = Integer.toString(bingo[i]);
                star = "*"; 
                System.out.print(star + "\t");
                break;
                } else if (t == i){
                System.out.print(bingo[i] + "\t");
                    } 
                }
                if (bingo[i] % 15 == 0 ){
                System.out.println();

                } if (bingo[i] == 15) {
                System.out.print("I" + "\t");
                } if (bingo[i] == 30) {
                System.out.print("N" + "\t");
                } if (bingo[i] == 45) {
                System.out.print("G" + "\t");
                } if (bingo[i] == 60) {
                System.out.print("O" + "\t");
                }
            }
        }    
              public static String randomCall(){
              Random object = new Random();
              int number = 1+object.nextInt(75); 

              if (number > 0 && number < 16 ){
              System.out.print("B" + number );
              } else if ( number > 15 && number < 31){
              System.out.print("I" + number );
              } else if ( number > 30 && number < 46){
              System.out.print("N" + number );
              } else if ( number > 45 && number < 61){
              System.out.print("G" + number );
              } else {
              System.out.print("O" + number );
              } 
              String real = Integer.toString(number);
              return real;
         }

           public static boolean checkWinner(String[] check){
           int b = 1;
           int i = 6;
           int n = 11;
           int g = 16;
           int o = 21;
           //vertical victory
           while(true){
           if (n == 13 && check[b].equals("*") && check[i].equals("*") && check[n].equals("F") && check[g].equals("*") && check[o].equals("*")|| 
               n == 38 && check[b].equals("*") && check[i].equals("*") && check[n].equals("F") && check[g].equals("*") && check[o].equals("*")){
           return true;
         } else if (check[b].equals("*") && check[i].equals("*") && check[n].equals("*") && check[g].equals("*") && check[o].equals("*")){
              return true;
               } else {
           if(b < 5 || b > 25 && b < 30) {
              b++; i++; n++; g++; o++;
             }else if( b == 5) {
              b+=21; i+=21; n+=21; g+=21; o+=21;
             } else {
             break;
                }
            }
        }
        //horizontal victory
        int m = 1;
        int p = 2;
        int x = 3;
        int u = 4;
        int w = 5;
     while (true){
      if (x == 13 && check[m].equals("*") && check[p].equals("*") && check[x].equals("F") && check[u].equals("*") && check[w].equals("*")|| 
          x == 38 && check[m].equals("*") && check[p].equals("*") && check[x].equals("F") && check[u].equals("*") && check[w].equals("*")){
        return true;
      } else if (check[m].equals("*") && check[p].equals("*") && check[x].equals("*") && check[u].equals("*") && check[w].equals("*")){
        return true;
      } else if (m < 46){
        m+=5; p+=5; x+=5; u+=5; w+=5;
      } else {
        break;
      }
  } //diagonal victory
          if (check[1].equals("*") && check[7].equals("*") && check[13].equals("F") && check[19].equals("*") && check[25].equals("*")||
              check[5].equals("*") && check[9].equals("*") && check[13].equals("F") && check[17].equals("*") && check[21].equals("*")||
              check[26].equals("*") && check[32].equals("*") && check[38].equals("F") && check[44].equals("*") && check[50].equals("*")||   
              check[30].equals("*") && check[34].equals("*") && check[38].equals("F") && check[42].equals("*") && check[46].equals("*")){
              return true;

              } else 
              return false;
          }  


            public static void main(String[] args)throws FileNotFoundException{

            int [] stars = new int[51];
            int [] starstwo = new int[51];
            int e = 0;
            String[] b = new String[51];
            String[] btwo = new String[51];
            int tob = 1;
            int i = 0;
            int itwo = 0;
            int cou = 1;
            int coutwo = 1;
            String star = "";
            String startwo = "";



            int totalBonus = 0;
            int bon =0;
            int secondChoice = 0;
            int thirdChoice = 0;

            for( int turns = 1; turns < 200; turns++){

            System.out.print("BINGO CALL IS: ");
            String replace = randomCall();
            int[] call = new int[76];
            int randomNum = Integer.parseInt(replace);
            call[turns] = randomNum;

            System.out.println();
            System.out.println();

                File player = new File("player.txt");
                Scanner second = new Scanner(player); 
                System.out.println("PLAYER CARDS:");
                //Reformatting the txt file
                System.out.format("%3s%4s%4s%4s%4s\t\t\t%3s%4s%4s%4s%4s%n", "B", "I", "N", "G", "O", "B", "I", "N", "G", "O");
                for (i= 1; i < 51; i++){
                for( cou = 1; cou < 51; cou++){
                if ( i == stars[cou]){
                b[i] = second.next();
                b[i] = "*";
                star = "*";
                System.out.printf("%4s", star + "\t");
                }
            }  
                if ( !star.equals("*")){
                b[i] = second.next();
                System.out.printf("%s", b[i] + "\t");
                }   
                if(star.equals("*")){
                star = "nothing";
                }
                 if(i == bon && !b[i].equals("*")){
                b[i]= second.next();
                b[i]= "F";
                System.out.printf("%s","F" + "\t");
            }
                 if(i % 5 == 0) {
                System.out.println();
                } 
            }   

                System.out.println();

                File cpuu = new File("cpu.txt");
                Scanner secondtwo = new Scanner(cpuu); 
                System.out.println("CPU CARDS:");
                //Reformatting the txt file
                System.out.format("%3s%4s%4s%4s%4s\t\t\t%3s%4s%4s%4s%4s%n", "B", "I", "N", "G", "O", "B", "I", "N", "G", "O");
                for (itwo= 1; itwo < 51; itwo++){
                for( coutwo = 1; coutwo < 51; coutwo++){
                if ( itwo == starstwo[coutwo]){
                btwo[itwo] = secondtwo.next();
                btwo[itwo] = "*";
                startwo = "*";
                System.out.printf("%4s", startwo + "\t");
                }
            }  
                if ( !startwo.equals("*")){
                btwo[itwo] = secondtwo.next();
                System.out.printf("%s", btwo[itwo] + "\t");
                }   
                if(startwo.equals("*")){
                startwo = "nothing";
                }
                 if(itwo == bon && !b[i].equals("*")){
                btwo[itwo] = second.next();
                btwo[itwo] = "F";
                System.out.printf("%s","F" + "\t");
            }
                 if(itwo % 5 == 0) {
                System.out.println();
                } 
            }   

            System.out.println();
            System.out.println();
            callBoard(call,turns);
            System.out.println();
            System.out.println("current points available: " + totalBonus + " point." );
            System.out.println("     1. Daub board");
            System.out.println("     2. Call Bingo!"); 
            System.out.println("     3. Use bonus (1000 points)");
            System.out.println("     4. Next call");
            Scanner input = new Scanner(System.in);
            long var = System.currentTimeMillis() / 1000;
            int choice = input.nextInt();

            if(choice == 1){
            long varl = System.currentTimeMillis() / 1000;


            if (varl - var <= 3){
            int bonus = 400;
            for ( e = 1; e < 51; e++){
            if ( replace.equals(b[e])){
            totalBonus+=bonus;
            System.out.println("you daubed " + replace + " on your board(s), " + bonus + " points earned.");
            stars[tob]=e;
            tob++;
            break;
            }else if(e==50){
             int penalty = 500;
             totalBonus-=penalty;
             System.out.println("You do not have "+ replace + " on your board(s), 500 points penalty");
            }
        }

        } else if(varl - var == 4) {
            int smallBonus = 200;
            for ( e = 1; e < 51; e++){
            if ( replace.equals(b[e])){
            totalBonus+=smallBonus;
            System.out.println("you daubed " + replace + " on your board(s), " + smallBonus + " points earned.");
            stars[tob]=e;
            tob++;
            break;
            }else if(e==50){
             int penalty = 500;
             totalBonus-=penalty;
             System.out.println("You do not have "+ replace + " on your board(s), 500 points penalty");
            }
        }

            }else{
            for ( e = 1; e < 51; e++){
            if ( replace.equals(b[e])){
            System.out.println("you daubed " + replace + " on your board(s).");
            stars[tob]=e;
            tob++;
            break;
            }else if(e==50){
             int penalty = 500;
             totalBonus-=penalty;
             System.out.println("You do not have "+ replace + " on your board(s), 500 points penalty");
                        }
                    }
                }
              secondChoice = input.nextInt(); 
          if (secondChoice == 2){
          if (checkWinner(b) == true){
            System.out.println("BINGO! congratulation, you have won!");
            System.exit(0);
                } else {
            System.out.println("CHEATER! you lose!");   
            System.exit(0);
                }
            }

            if(secondChoice == 3){

            if( totalBonus >= 1000 ){
           Random object = new Random();
             bon = 1+object.nextInt(50); 
             for(int w = 0; w < 10; w++){
             if( b[bon].equals("*")){
            bon = 1+object.nextInt(50);
                 }
             } System.out.println("Bonus daubs at " + b[bon]);
            if (!b[bon].equals("*")) {
            System.out.println("Bonus daubs at " + b[bon]);
            }
        } else {
        System.out.println("Not enough Bonus points.");
         }
         thirdChoice = input.nextInt();
    }      

            if (secondChoice == 4 || thirdChoice == 4){
         for ( k = 1; k < 51; k++){
            if ( replace.equals(starsCpu[k])){
             System.out.println("The computer daubed " + starsCpu[k]);
            starsCpu[com] = k;      
                com++;
                    if (checkWinner(a) == true){
                    System.out.println("BINGO! , computer has won!");
                    }
                    }else if (k == 50){
                    System.out.println("The computer do not have " + replace);
                    }
                }
            }
        }//end of first choice    
        }
    }
}
4

1 回答 1

0

当您写入时,您正在读取并丢弃文件中的大量内容

    if ( itwo == starstwo[coutwo]){
            btwo[itwo] = secondtwo.next();
            btwo[itwo] = "*";

然后,稍后当你写

            btwo[itwo] = secondtwo.next();
            System.out.printf("%s", btwo[itwo] + "\t");

没有什么可读的了。

但我仍然不明白这如何导致你的文本文件被删除和替换,就像你说的那样。您的示例中没有任何代码会擦除或写入您的文本文件。

于 2013-12-19T20:08:50.923 回答