public static void main(String[] args) {

    int inputNumber; // One of the integers input by the user.
    int sum; // The sum of the positive integers.
    int count; // The number of positive integers.
    double average; // The average of the positive integers.

    /* Initialize the summation and counting variables. */

    sum = 0;
    count = 0;

    /* Read and process the user's input. */

    TextIO.put("Enter your first positive integer: ");
    inputNumber = TextIO.getlnInt();

    while (inputNumber != 0) {
      sum += inputNumber; // Add inputNumber to running sum.
      count++; // Count the input by adding 1 to count.
      TextIO.put("Enter your next positive integer, or 0 to end: ");
      inputNumber = TextIO.getlnInt();
    }

    /* Display the result. */

    if (count == 0) {
      TextIO.putln("You didn't enter any data!");
    } else {
      average = ((double) sum) / count;
      TextIO.putln();
      TextIO.putln("You entered " + count + " positive integers.");
      TextIO.putf("Their average is %1.3f.\n", average);
    }
  } // end main()
Example #2
0
  /** Test of makeMove method, of class Position. */
  public void testPromotion() throws ChessParseError {
    String fen = "r1bqk2r/1Pppbppp/p1n2n2/2P1p3/B3P3/5N2/Pp1P1PPP/R1BQK2R w KQkq - 0 1";
    Position pos = TextIO.readFEN(fen);
    assertEquals(fen, TextIO.toFEN(pos));
    Position origPos = new Position(pos);
    assertEquals(origPos, pos);

    Move move = new Move(Position.getSquare(1, 6), Position.getSquare(0, 7), Piece.WQUEEN);
    UndoInfo ui = new UndoInfo();
    pos.makeMove(move, ui);
    assertEquals(Piece.EMPTY, pos.getPiece(Position.getSquare(1, 6)));
    assertEquals(Piece.WQUEEN, pos.getPiece(Position.getSquare(0, 7)));
    pos.unMakeMove(move, ui);
    assertEquals(origPos, pos);

    move = new Move(Position.getSquare(1, 6), Position.getSquare(1, 7), Piece.WKNIGHT);
    ui = new UndoInfo();
    pos.makeMove(move, ui);
    assertEquals(Piece.EMPTY, pos.getPiece(Position.getSquare(1, 6)));
    assertEquals(Piece.WKNIGHT, pos.getPiece(Position.getSquare(1, 7)));
    pos.unMakeMove(move, ui);
    assertEquals(origPos, pos);

    pos.setWhiteMove(false);
    origPos = new Position(pos);

    move = new Move(Position.getSquare(1, 1), Position.getSquare(2, 0), Piece.BROOK);
    ui = new UndoInfo();
    pos.makeMove(move, ui);
    assertEquals(Piece.EMPTY, pos.getPiece(Position.getSquare(1, 1)));
    assertEquals(Piece.BROOK, pos.getPiece(Position.getSquare(2, 0)));
    pos.unMakeMove(move, ui);
    assertEquals(origPos, pos);
  }
Example #3
0
  public void testNullMove() throws ChessParseError {
    Move nullMove = new Move(0, 0, 0);
    UndoInfo ui = new UndoInfo();

    Position pos = TextIO.readFEN(TextIO.startPosFEN);
    Position origPos = new Position(pos);
    pos.makeMove(nullMove, ui);
    assertEquals(false, pos.whiteMove);
    assertEquals(true, pos.a1Castle());
    assertEquals(Piece.WROOK, pos.getPiece(0));
    pos.unMakeMove(nullMove, ui);
    assertTrue(pos.equals(origPos));

    pos = TextIO.readFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w Kkq - 5 1");
    origPos = new Position(pos);
    pos.makeMove(nullMove, ui);
    assertEquals(false, pos.whiteMove);
    assertEquals(false, pos.a1Castle());
    assertEquals(Piece.WROOK, pos.getPiece(0));
    assertEquals(0, pos.halfMoveClock);
    pos.unMakeMove(nullMove, ui);
    assertTrue(pos.equals(origPos));

    pos = TextIO.readFEN("7k/8/8/8/8/8/4P3/K7 w - - 5 1");
    origPos = new Position(pos);
    pos.makeMove(nullMove, ui);
    assertEquals(false, pos.whiteMove);
    assertEquals(false, pos.a1Castle());
    assertEquals(0, pos.halfMoveClock);
    assertEquals(0, pos.getKingSq(true));
    assertEquals(63, pos.getKingSq(false));

    UndoInfo ui2 = new UndoInfo();
    pos.makeMove(nullMove, ui2);
    assertEquals(true, pos.whiteMove);
    assertEquals(false, pos.a1Castle());
    assertEquals(0, pos.halfMoveClock);
    assertEquals(0, pos.getKingSq(true));
    assertEquals(63, pos.getKingSq(false));

    pos.unMakeMove(nullMove, ui2);
    assertEquals(false, pos.whiteMove);
    assertEquals(false, pos.a1Castle());
    assertEquals(0, pos.halfMoveClock);
    assertEquals(0, pos.getKingSq(true));
    assertEquals(63, pos.getKingSq(false));

    pos.unMakeMove(nullMove, ui);
    assertTrue(pos.equals(origPos));

    pos = TextIO.readFEN("5k2/8/4R1K1/8/8/8/8/8 b - - 5 1");
    origPos = new Position(pos);
    pos.makeMove(nullMove, ui);
    assertEquals(true, pos.whiteMove);
    assertEquals(false, pos.a1Castle());
    assertEquals(0, pos.halfMoveClock);

    pos.unMakeMove(nullMove, ui);
    assertTrue(pos.equals(origPos));
  }
Example #4
0
  @SuppressWarnings("static-access")
  public static void main(String[] args) {

    TextIO reader = new TextIO();
    reader.readFile("encodedFile.in");
    int numOfLines = reader.getInt();
    String string = "";
    String finish = "";

    // System.out.println(numOfLines);

    //		char c = 'A';
    //		int b = (int) c;
    //		System.out.println(b + " casting");

    while (numOfLines != -1) {
      numOfLines--;
      string = reader.getlnString();

      for (int i = 0; i < string.length(); i++) {
        char cahar = string.charAt(i);

        // System.out.println(cahar);
        int intcar = (int) cahar;
        int correctint = intcar - 1;
        char correctchar = (char) correctint;
        // System.out.println(correctchar);
        finish += correctchar;
      }
    }

    //	System.out.println(string);

    System.out.println(finish);
  }
 /**
  * Debug ME! Use the unit tests to reverse engineer how this method should work. Hint: Fix the
  * formatting (shift-cmd-F) to help debug the following code
  *
  * @param list
  * @param maximum
  */
 public static void list(String[] list, int maximum) {
   int i;
   // for ( i = 0      ; i    <=    maximum; ); {      TextIO.putln(   ""  + i + ". " + list[i]);}
   if (maximum > 0) { // show some of items
     for (i = 0; i < maximum; i++) TextIO.putln("" + (i + 1) + ". " + list[i]);
   } else { // show all items
     for (i = 0; i < list.length; i++) TextIO.putln("" + (i + 1) + ". " + list[i]);
   }
 }
Example #6
0
  public static void main(String[] args) {

    int eggs; // Number of eggs, input by user.
    int gross; // How many gross in that number of eggs?
    int aboveGross; // How many eggs are left over, above an
    //    integral number of gross?  This number
    //    can be computed as eggs % 144, and is
    //    in the range 0 to 143.  This number will
    //    be divided into dozens and extras.
    int dozens; // How many dozens in aboveGross?
    int extras; // How many eggs are left over, above integral
    //    numbers of gross and dozens?

    TextIO.put("How many eggs do you have?  ");
    eggs = TextIO.getlnInt();

    gross = eggs / 144;
    aboveGross = eggs % 144;

    dozens = aboveGross / 12;
    extras = aboveGross % 12;

    TextIO.put("Your number of eggs is ");
    TextIO.put(gross);
    TextIO.put(" gross, ");
    TextIO.put(dozens);
    TextIO.put(" dozen, and ");
    TextIO.put(extras);
    TextIO.putln();
  } // end main()
  // Lecture 7 example
  // Print 'You Win' if the first and last letter are the same
  public static void main(String[] args) {
    TextIO.put("Enter a string");
    String s = TextIO.getln();
    // ask the string to create a new string
    // change s to point to (hold the memory address of)
    // the newly created string
    s = s.toLowerCase();

    char first = s.charAt(0);
    char last = s.charAt(s.length() - 1);
    if (first == last) TextIO.putln("You win!");
  }
Example #8
0
  /** Test of hashCode method, of class Position. */
  public void testHashCode() throws ChessParseError {
    Position pos = TextIO.readFEN(TextIO.startPosFEN);
    long h1 = pos.zobristHash();
    assertEquals(h1, pos.computeZobristHash());
    UndoInfo ui = new UndoInfo();
    Move move = TextIO.stringToMove(pos, "e4");
    pos.makeMove(move, ui);
    assertTrue(h1 != pos.zobristHash());
    pos.unMakeMove(move, ui);
    assertTrue(h1 == pos.zobristHash());

    pos.setWhiteMove(!pos.whiteMove);
    long h4 = pos.zobristHash();
    assertEquals(h4, pos.computeZobristHash());
    assertTrue(h1 != pos.zobristHash());
    pos.setWhiteMove(!pos.whiteMove);
    assertTrue(h1 == pos.zobristHash());

    pos.setCastleMask(0);
    assertTrue(h1 != pos.zobristHash());

    pos = TextIO.readFEN("rnbqkbnr/pppp1ppp/8/2P1p3/8/8/PP1PPPPP/RNBQKBNR b KQkq - 0 1");
    h1 = pos.zobristHash();
    assertEquals(h1, pos.computeZobristHash());

    String[] moves = {
      "b5", "Nc3", "Nf6", "Nb1", "Ng8", "Nc3", "Nf6", "Nb1", "Ng8", "Nc3", "d5", "cxd6", "Qxd6",
      "h4", "Be6", "h5", "Nc6", "h6", "o-o-o", "hxg7", "Nf6", "gxh8Q", "Be7"
    };
    List<UndoInfo> uiList = new ArrayList<UndoInfo>();
    List<Long> hashList = new ArrayList<Long>();
    List<Move> moveList = new ArrayList<Move>();
    for (int i = 0; i < moves.length; i++) {
      uiList.add(new UndoInfo());
      Move m = TextIO.stringToMove(pos, moves[i]);
      moveList.add(m);
      pos.makeMove(m, uiList.get(i));
      long h = pos.zobristHash();
      assertEquals(h, pos.computeZobristHash());
      hashList.add(h);
    }
    assertTrue(!hashList.get(0).equals(hashList.get(4)));
    assertTrue(hashList.get(4).equals(hashList.get(8)));
    for (int i = moves.length - 1; i >= 0; i--) {
      pos.unMakeMove(moveList.get(i), uiList.get(i));
      long h = pos.zobristHash();
      assertEquals(h, pos.computeZobristHash());
      assertEquals(h, i > 0 ? hashList.get(i - 1) : h1);
    }
  }
Example #9
0
  /**
   * print3NSequence prints a 3N+1 sequence to standard output, using startingValue as the initial
   * value of N. It also prints the number of terms in the sequence. The value of the parameter,
   * startingValue, must be a positive integer.
   */
  static void print3NSequence(int startingValue) {

    int N; // One of the terms in the sequence.
    int count; // The number of terms found.
    int onLine; // The number of terms that have been output
    //     so far on the current line.

    N = startingValue; // Start the sequence with startingValue;
    count = 1; // We have one term so far.

    TextIO.putln("The 3N+1 sequence starting from " + N);
    TextIO.putln();
    TextIO.put(N, 8); // Print initial term, using 8 characters.
    onLine = 1; // There's now 1 term on current output line.

    while (N > 1) {
      N = nextN(N); // compute next term
      count++; // count this term
      if (onLine == 5) { // If current output line is full
        TextIO.putln(); // ...then output a carriage return
        onLine = 0; // ...and note that there are no terms
        //               on the new line.
      }
      TextIO.putf("%8d", N); // Print this term in an 8-char column.
      onLine++; // Add 1 to the number of terms on this line.
    }

    TextIO.putln(); // end current line of output
    TextIO.putln(); // and then add a blank line
    TextIO.putln("There were " + count + " terms in the sequence.");
  } // end of Print3NSequence
Example #10
0
 /**
  * Reads a factor from the current line of input and builds an expression tree that represents the
  * expression.
  *
  * @return an ExpNode which is a pointer to the root node of the expression tree
  * @throws ParseError if a syntax error is found in the input
  */
 private static ExpNode factorTree() throws ParseError {
   TextIO.skipBlanks();
   char ch = TextIO.peek();
   if (Character.isDigit(ch)) {
     // The factor is a number.  Return a ConstNode.
     double num = TextIO.getDouble();
     return new ConstNode(num);
   } else if (ch == 'x' || ch == 'X') {
     // The factor is the variable x.
     TextIO.getAnyChar(); // Read the X.
     return new VariableNode();
   } else if (ch == '(') {
     // The factor is an expression in parentheses.
     // Return a tree representing that expression.
     TextIO.getAnyChar(); // Read the "("
     ExpNode exp = expressionTree();
     TextIO.skipBlanks();
     if (TextIO.peek() != ')') throw new ParseError("Missing right parenthesis.");
     TextIO.getAnyChar(); // Read the ")"
     return exp;
   } else if (ch == '\n')
     throw new ParseError("End-of-line encountered in the middle of an expression.");
   else if (ch == ')') throw new ParseError("Extra right parenthesis.");
   else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
     throw new ParseError("Misplaced operator.");
   else throw new ParseError("Unexpected character \"" + ch + "\" encountered.");
 } // end factorTree()
Example #11
0
  public static void main(String[] args) {

    String str; // Line of text entered by the user.
    int count; // Number of different letters found in str.
    char letter; // A letter of the alphabet.

    System.out.println("Please type in a line of text.");
    str = TextIO.getln();

    str = str.toUpperCase();

    count = 0;
    System.out.println("Your input contains the following letters:");
    System.out.println();
    System.out.print("   ");
    for (letter = 'A'; letter <= 'Z'; letter++) {
      int i; // Position of a char in str
      for (i = 0; i < str.length(); i++) {
        if (letter == str.charAt(i)) {
          System.out.print(letter);
          System.out.print(' ');
          count++;
          break;
        }
      }
    }

    System.out.println();
    System.out.println();
    System.out.println("There were " + count + " different letteres.");
  }
Example #12
0
  @Test
  public void readList1() throws Exception {
    String inputText = "10\n" + "8\n" + "6\n" + "4\n" + "2\n" + "0\n";

    VType value = TextIO.readList(new StringReader(inputText));
    assertThat(value, instanceOf(VNumberArray.class));
    VNumberArray array = (VNumberArray) value;
    assertThat(array.getData(), equalTo((ListNumber) new ArrayDouble(10, 8, 6, 4, 2, 0)));
  }
Example #13
0
  @Test
  public void readList2() throws Exception {
    String inputText = "A\n" + "B\n" + "C\n" + "D\n" + "E\n" + "F\n";

    VType value = TextIO.readList(new StringReader(inputText));
    assertThat(value, instanceOf(VStringArray.class));
    VStringArray array = (VStringArray) value;
    assertThat(array.getData(), equalTo(Arrays.asList("A", "B", "C", "D", "E", "F")));
  }
Example #14
0
  public static void main(String[] args) {

    while (true) {
      System.out.println("\n\nEnter an expression, or press return to end.");
      System.out.print("\n?  ");
      TextIO.skipBlanks();
      if (TextIO.peek() == '\n') break;
      try {
        ExpNode exp = expressionTree();
        TextIO.skipBlanks();
        if (TextIO.peek() != '\n') throw new ParseError("Extra data after end of expression.");
        TextIO.getln();
        ExpNode deriv = exp.derivative();
        System.out.println("\nA fully parenthesized expression for the derivative is:");
        System.out.print("   ");
        deriv.printInfix();
        System.out.println();
        System.out.println("\nValue of derivative at x = 0 is " + deriv.value(0));
        System.out.println("Value of derivative at x = 1 is " + deriv.value(1));
        System.out.println("Value of derivative at x = 2 is " + deriv.value(2));
        System.out.println("Value of derivative at x = 3 is " + deriv.value(3));
        System.out.println("\nOrder of postfix evaluation for derivative is:\n");
        deriv.printStackCommands();
      } catch (ParseError e) {
        System.out.println("\n*** Error in input:    " + e.getMessage());
        System.out.println("*** Discarding input:  " + TextIO.getln());
      }
    }

    System.out.println("\n\nDone.");
  } // end main()
Example #15
0
 public static void main(String[] args) {
   System.out.println("\n\n Plese select input files");
   try {
     if (TextIO.readUserSelectedFilesonebyone() == false) {
       System.out.println("No input file selected.Exiting.");
       System.exit(1);
     }
   } catch (Exception e) {
     System.out.println("Sorry, an error has occurred.");
     System.out.println("Error Message:  " + e.getMessage());
     e.printStackTrace();
   }
 }
  /** Test of insert method, of class TranspositionTable. */
  @Test
  public void testInsert() throws ChessParseError {
    System.out.println("insert");
    TranspositionTable tt = new TranspositionTable(16);
    Position pos = TextIO.readFEN(TextIO.startPosFEN);
    String[] moves = {
      "e4", "e5", "Nf3", "Nc6", "Bb5", "a6", "Ba4", "b5", "Bb3", "Nf6", "O-O", "Be7", "Re1"
    };
    UndoInfo ui = new UndoInfo();
    for (int i = 0; i < moves.length; i++) {
      Move m = TextIO.stringToMove(pos, moves[i]);
      pos.makeMove(m, ui);
      int score = i * 17 + 3;
      m.score = score;
      int type = TranspositionTable.TTEntry.T_EXACT;
      int ply = i + 1;
      int depth = i * 2 + 5;
      tt.insert(pos.historyHash(), m, type, ply, depth, score * 2 + 3);
    }

    pos = TextIO.readFEN(TextIO.startPosFEN);
    for (int i = 0; i < moves.length; i++) {
      Move m = TextIO.stringToMove(pos, moves[i]);
      pos.makeMove(m, ui);
      TranspositionTable.TTEntry ent = tt.probe(pos.historyHash());
      assertEquals(TranspositionTable.TTEntry.T_EXACT, ent.type);
      int score = i * 17 + 3;
      int ply = i + 1;
      int depth = i * 2 + 5;
      assertEquals(score, ent.getScore(ply));
      assertEquals(depth, ent.getDepth());
      assertEquals(score * 2 + 3, ent.evalScore);
      Move tmpMove = new Move(0, 0, 0);
      ent.getMove(tmpMove);
      assertEquals(m, tmpMove);
    }
  }
Example #17
0
 private static String readNextWord() {
   char ch = TextIO.peek(); // Look at next character in input.
   while (ch != TextIO.EOF && !Character.isLetterOrDigit(ch)) {
     TextIO.getAnyChar(); // Read the character.
     ch = TextIO.peek(); // Look at the next character.
   }
   if (ch == TextIO.EOF) // Encountered end-of-file
   return null;
   // At this point, we know that the next character, so read a word.
   String word = ""; // This will be the word that is read.
   while (true) {
     word += TextIO.getAnyChar(); // Append the letter onto word.
     ch = TextIO.peek(); // Look at next character.
     if (ch == '\'') {
       // The next character is an apostrophe.  Read it, and
       // if the following character is a letter, add both the
       // apostrophe and the letter onto the word and continue
       // reading the word.  If the character after the apostrophe
       // is not a letter, the word is done, so break out of the loop.
       TextIO.getAnyChar(); // Read the apostrophe.
       ch = TextIO.peek(); // Look at char that follows apostrophe.
       if (Character.isLetter(ch)) {
         word += "\'" + TextIO.getAnyChar();
         ch = TextIO.peek(); // Look at next char.
       } else break;
     }
     if (!Character.isLetterOrDigit(ch)
         && ch != '@'
         && ch != '.'
         && ch != '_'
         && ch != '/'
         && ch != ':') {
       // If the next character is not a letter, the word is
       // finished, so bread out of the loop.
       break;
     }
     // If we haven't broken out of the loop, next char is a letter.
   }
   return word; // Return the word that has been read.
 }
Example #18
0
  /** Test of getPiece method, of class Position. */
  public void testGetPiece() throws ChessParseError {
    Position pos = new Position();
    int result = pos.getPiece(0);
    assertEquals(result, Piece.EMPTY);

    pos = TextIO.readFEN(TextIO.startPosFEN);
    result = pos.getPiece(0);
    assertEquals(result, Piece.WROOK);
    for (int x = 0; x < 8; x++) {
      for (int y = 0; y < 2; y++) {
        int p1 = pos.getPiece(Position.getSquare(x, y));
        int p2 = pos.getPiece(Position.getSquare(x, 7 - y));
        int bwDiff = Piece.BPAWN - Piece.WPAWN;
        assertEquals(p2, p1 + bwDiff);
      }
    }
  }
Example #19
0
 /**
  * Example output if user enters 10: Max? 1 + 3 + 5 + 7 + 9 = 25 25 = 9 + 7 + 5 + 3 + 1
  *
  * <p>Example output if user enters 11: Max? 1 + 3 + 5 + 7 + 9 + 11 = 36 36 = 11 + 9 + 7 + 5 + 3 +
  * 1
  */
 public static void main(String[] args) {
   TextIO.putln("Max?");
   int max = TextIO.getlnInt();
   int sum = 0;
   for (int i = 0; i <= max; i++) {
     if (i % 2 == 1) {
       sum = sum + i;
       if (i == max || i == max - 1) TextIO.put(i + " = ");
       else TextIO.put(i + " + ");
     }
   }
   TextIO.putln(sum);
   TextIO.put(sum + " = ");
   for (int j = max; j > 0; j--) {
     if (j % 2 == 1) {
       if (j == 1) TextIO.put(j);
       else TextIO.put(j + " + ");
     }
   }
 } // end of main method
Example #20
0
 @Test
 public void writeList2() throws Exception {
   VType vType = ValueFactory.toVType(Arrays.asList("A", "B", "C", "D", "E"));
   StringWriter writer = new StringWriter();
   TextIO.writeList(vType, writer);
   writer.flush();
   assertThat(
       writer.toString(),
       equalTo(
           "A"
               + System.lineSeparator()
               + "B"
               + System.lineSeparator()
               + "C"
               + System.lineSeparator()
               + "D"
               + System.lineSeparator()
               + "E"
               + System.lineSeparator()));
 }
Example #21
0
 /**
  * Reads an expression from the current line of input and builds an expression tree that
  * represents the expression.
  *
  * @return an ExpNode which is a pointer to the root node of the expression tree
  * @throws ParseError if a syntax error is found in the input
  */
 private static ExpNode expressionTree() throws ParseError {
   TextIO.skipBlanks();
   boolean negative; // True if there is a leading minus sign.
   negative = false;
   if (TextIO.peek() == '-') {
     TextIO.getAnyChar();
     negative = true;
   }
   ExpNode exp; // The expression tree for the expression.
   exp = termTree(); // Start with the first term.
   if (negative) exp = new UnaryMinusNode(exp);
   TextIO.skipBlanks();
   while (TextIO.peek() == '+' || TextIO.peek() == '-') {
     // Read the next term and combine it with the
     // previous terms into a bigger expression tree.
     char op = TextIO.getAnyChar();
     ExpNode nextTerm = termTree();
     exp = new BinOpNode(op, exp, nextTerm);
     TextIO.skipBlanks();
   }
   return exp;
 } // end expressionTree()
Example #22
0
 /**
  * Reads a term from the current line of input and builds an expression tree that represents the
  * expression.
  *
  * @return an ExpNode which is a pointer to the root node of the expression tree
  * @throws ParseError if a syntax error is found in the input
  */
 private static ExpNode termTree() throws ParseError {
   TextIO.skipBlanks();
   ExpNode term; // The expression tree representing the term.
   term = factorTree();
   TextIO.skipBlanks();
   while (TextIO.peek() == '*' || TextIO.peek() == '/') {
     // Read the next factor, and combine it with the
     // previous factors into a bigger expression tree.
     char op = TextIO.getAnyChar();
     ExpNode nextFactor = factorTree();
     term = new BinOpNode(op, term, nextFactor);
     TextIO.skipBlanks();
   }
   return term;
 } // end termValue()
Example #23
0
  public static void main(String[] args) {

    TextIO.putln("This program will print out 3N+1 sequences");
    TextIO.putln("for starting values that you specify.");
    TextIO.putln();

    int K; // Starting point for sequence, specified by the user.
    do {
      TextIO.putln("Enter a starting value;");
      TextIO.put("To end the program, enter 0: ");
      K = TextIO.getInt(); // get starting value from user
      if (K > 0) // print sequence, but only if K is > 0
      print3NSequence(K);
    } while (K > 0); // continue only if K &gt; 0
  } // end main
Example #24
0
  /** @param args */
  public static void main(String[] args) {
    // Formatted output
    double third = 1.0 / 3;
    TextIO.put("One third is ");
    TextIO.put(third);
    TextIO.putf(", or %.2f", third);
    TextIO.putln(" OK?");

    // A program that prints out if a number is odd or even
    // However it lies for some values...
    boolean oops = false;
    int a = 0;
    while (a <= 10) {
      oops = a >= 4 && a <= 8;
      TextIO.put(a + " is ");
      if ((a % 2 == 1) || oops) TextIO.putln("Odd");
      else TextIO.putln("Even");
      a++;
    }
  }
Example #25
0
 @Test
 public void writeList1() throws Exception {
   VType vType = ValueFactory.toVType(new ArrayDouble(10, 8, 6, 4, 2, 0));
   StringWriter writer = new StringWriter();
   TextIO.writeList(vType, writer);
   writer.flush();
   assertThat(
       writer.toString(),
       equalTo(
           "10.0"
               + System.lineSeparator()
               + "8.0"
               + System.lineSeparator()
               + "6.0"
               + System.lineSeparator()
               + "4.0"
               + System.lineSeparator()
               + "2.0"
               + System.lineSeparator()
               + "0.0"
               + System.lineSeparator()));
 }
Example #26
0
  /** Test of makeMove method, of class Position. */
  public void testMakeMove() throws ChessParseError {
    Position pos = TextIO.readFEN(TextIO.startPosFEN);
    Position origPos = new Position(pos);
    assertTrue(pos.equals(origPos));
    Move move = new Move(Position.getSquare(4, 1), Position.getSquare(4, 3), Piece.EMPTY);
    UndoInfo ui = new UndoInfo();
    pos.makeMove(move, ui);
    assertEquals(pos.whiteMove, false);
    assertEquals(-1, pos.getEpSquare());
    assertEquals(Piece.EMPTY, pos.getPiece(Position.getSquare(4, 1)));
    assertEquals(Piece.WPAWN, pos.getPiece(Position.getSquare(4, 3)));
    assertTrue(!pos.equals(origPos));
    int castleMask =
        (1 << Position.A1_CASTLE)
            | (1 << Position.H1_CASTLE)
            | (1 << Position.A8_CASTLE)
            | (1 << Position.H8_CASTLE);
    assertEquals(castleMask, pos.getCastleMask());
    pos.unMakeMove(move, ui);
    assertEquals(pos.whiteMove, true);
    assertEquals(Piece.WPAWN, pos.getPiece(Position.getSquare(4, 1)));
    assertEquals(Piece.EMPTY, pos.getPiece(Position.getSquare(4, 3)));
    assertTrue(pos.equals(origPos));

    String fen = "r1bqk2r/2ppbppp/p1n2n2/1pP1p3/B3P3/5N2/PP1P1PPP/RNBQK2R w KQkq b6 0 2";
    pos = TextIO.readFEN(fen);
    assertEquals(fen, TextIO.toFEN(pos));
    origPos = new Position(pos);
    assertEquals(Position.getSquare(1, 5), pos.getEpSquare());

    // Test capture
    move = new Move(Position.getSquare(0, 3), Position.getSquare(1, 4), Piece.EMPTY);
    pos.makeMove(move, ui);
    assertEquals(-1, pos.getEpSquare());
    assertEquals(Piece.WBISHOP, pos.getPiece(Position.getSquare(1, 4)));
    assertEquals(Piece.EMPTY, pos.getPiece(Position.getSquare(0, 3)));
    pos.unMakeMove(move, ui);
    assertTrue(pos.equals(origPos));

    // Test castling
    move = new Move(Position.getSquare(4, 0), Position.getSquare(6, 0), Piece.EMPTY);
    pos.makeMove(move, ui);
    assertEquals(Piece.WROOK, pos.getPiece(Position.getSquare(5, 0)));
    assertEquals(Piece.EMPTY, pos.getPiece(Position.getSquare(7, 0)));
    castleMask = (1 << Position.A8_CASTLE) | (1 << Position.H8_CASTLE);
    assertEquals(castleMask, pos.getCastleMask());
    assertEquals(-1, pos.getEpSquare());
    pos.unMakeMove(move, ui);
    assertTrue(pos.equals(origPos));

    // Test castling rights (king move)
    move = new Move(Position.getSquare(4, 0), Position.getSquare(4, 1), Piece.EMPTY);
    pos.makeMove(move, ui);
    castleMask = (1 << Position.A8_CASTLE) | (1 << Position.H8_CASTLE);
    assertEquals(castleMask, pos.getCastleMask());
    assertEquals(-1, pos.getEpSquare());
    pos.unMakeMove(move, ui);
    assertTrue(pos.equals(origPos));

    // Test castling rights (rook move)
    move = new Move(Position.getSquare(7, 0), Position.getSquare(6, 0), Piece.EMPTY);
    pos.makeMove(move, ui);
    castleMask = (1 << Position.A1_CASTLE) | (1 << Position.A8_CASTLE) | (1 << Position.H8_CASTLE);
    assertEquals(castleMask, pos.getCastleMask());
    assertEquals(-1, pos.getEpSquare());
    pos.unMakeMove(move, ui);
    assertTrue(pos.equals(origPos));

    // Test en passant
    move = new Move(Position.getSquare(2, 4), Position.getSquare(1, 5), Piece.EMPTY);
    pos.makeMove(move, ui);
    assertEquals(Piece.WPAWN, pos.getPiece(Position.getSquare(1, 5)));
    assertEquals(Piece.EMPTY, pos.getPiece(Position.getSquare(2, 4)));
    assertEquals(Piece.EMPTY, pos.getPiece(Position.getSquare(1, 4)));
    pos.unMakeMove(move, ui);
    assertTrue(pos.equals(origPos));

    // Test castling rights loss when rook captured
    pos.setPiece(Position.getSquare(6, 2), Piece.BKNIGHT);
    pos.setWhiteMove(false);
    Position origPos2 = new Position(pos);
    move = new Move(Position.getSquare(6, 2), Position.getSquare(7, 0), Piece.EMPTY);
    pos.makeMove(move, ui);
    castleMask = (1 << Position.A1_CASTLE) | (1 << Position.A8_CASTLE) | (1 << Position.H8_CASTLE);
    assertEquals(castleMask, pos.getCastleMask());
    assertEquals(-1, pos.getEpSquare());
    pos.unMakeMove(move, ui);
    assertTrue(pos.equals(origPos2));
  }
Example #27
0
 /** Test of getKingSq method, of class Position. */
 public void testGetKingSq() throws ChessParseError {
   Position pos = TextIO.readFEN(TextIO.startPosFEN);
   assertEquals(TextIO.getSquare("e1"), pos.getKingSq(true));
   assertEquals(TextIO.getSquare("e8"), pos.getKingSq(false));
   pos = TextIO.readFEN("r1bq1bnr/ppppkppp/2n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R w KQ - 0 4");
   assertEquals(TextIO.getSquare("e1"), pos.getKingSq(true));
   assertEquals(TextIO.getSquare("e7"), pos.getKingSq(false));
   UndoInfo ui = new UndoInfo();
   pos.makeMove(TextIO.stringToMove(pos, "o-o"), ui);
   assertEquals(TextIO.getSquare("g1"), pos.getKingSq(true));
   assertEquals(TextIO.getSquare("e7"), pos.getKingSq(false));
   pos.makeMove(TextIO.stringToMove(pos, "Kd6"), ui);
   assertEquals(TextIO.getSquare("g1"), pos.getKingSq(true));
   assertEquals(TextIO.getSquare("d6"), pos.getKingSq(false));
 }
Example #28
0
  /** Test of drawRuleEquals, of class Position. */
  public void testDrawRuleEquals() throws ChessParseError {
    Position pos = TextIO.readFEN(TextIO.startPosFEN);
    Position origPos = new Position(pos);
    UndoInfo ui = new UndoInfo();
    pos.makeMove(TextIO.stringToMove(pos, "Nf3"), ui);
    assertEquals(false, pos.drawRuleEquals(origPos));
    pos.makeMove(TextIO.stringToMove(pos, "Nf6"), ui);
    assertEquals(false, pos.drawRuleEquals(origPos));
    pos.makeMove(TextIO.stringToMove(pos, "Ng1"), ui);
    assertEquals(false, pos.drawRuleEquals(origPos));
    pos.makeMove(TextIO.stringToMove(pos, "Ng8"), ui);
    assertEquals(true, pos.drawRuleEquals(origPos));
    assertEquals(false, pos.equals(origPos)); // Move counters have changed

    String fen = "r1bqkb1r/pppp1ppp/2n2n2/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 0 1";
    pos = TextIO.readFEN(fen);
    origPos = new Position(pos);
    pos.makeMove(TextIO.stringToMove(pos, "Ke2"), ui);
    assertEquals(false, pos.drawRuleEquals(origPos));
    pos.makeMove(TextIO.stringToMove(pos, "Be7"), ui);
    assertEquals(false, pos.drawRuleEquals(origPos));
    pos.makeMove(TextIO.stringToMove(pos, "Ke1"), ui);
    assertEquals(false, pos.drawRuleEquals(origPos));
    pos.makeMove(TextIO.stringToMove(pos, "Bf8"), ui);
    assertEquals(false, pos.drawRuleEquals(origPos)); // Not equal, castling rights lost

    pos = TextIO.readFEN(TextIO.startPosFEN);
    pos.makeMove(TextIO.stringToMove(pos, "c4"), ui);
    pos.makeMove(TextIO.stringToMove(pos, "a6"), ui);
    pos.makeMove(TextIO.stringToMove(pos, "c5"), ui);
    pos.makeMove(TextIO.stringToMove(pos, "b5"), ui);
    assertEquals(Position.getSquare(1, 5), pos.getEpSquare());
    origPos = new Position(pos);
    pos.makeMove(TextIO.stringToMove(pos, "Nc3"), ui);
    pos.makeMove(TextIO.stringToMove(pos, "Nc6"), ui);
    pos.makeMove(TextIO.stringToMove(pos, "Nb1"), ui);
    pos.makeMove(TextIO.stringToMove(pos, "Nb8"), ui);
    assertEquals(false, pos.drawRuleEquals(origPos)); // Not equal, en passant rights lost
  }
Example #29
0
  /** Test move counters, of class Position. */
  public void testMoveCounters() throws ChessParseError {
    String fen = "r1bqk2r/2ppbppp/p1n2n2/1pP1p3/B3P3/5N2/PP1P1PPP/RNBQK2R w KQkq b6 0 7";
    Position pos = TextIO.readFEN(fen);

    Move move = TextIO.stringToMove(pos, "Nc3");
    UndoInfo ui = new UndoInfo();
    pos.makeMove(move, ui);
    assertEquals(1, pos.halfMoveClock);
    assertEquals(7, pos.fullMoveCounter);
    pos.unMakeMove(move, ui);

    move = TextIO.stringToMove(pos, "O-O");
    pos.makeMove(move, ui);
    assertEquals(1, pos.halfMoveClock); // Castling does not reset 50 move counter
    assertEquals(7, pos.fullMoveCounter);
    pos.unMakeMove(move, ui);

    move = TextIO.stringToMove(pos, "a3");
    pos.makeMove(move, ui);
    assertEquals(0, pos.halfMoveClock); // Pawn move resets 50 move counter
    assertEquals(7, pos.fullMoveCounter);
    pos.unMakeMove(move, ui);

    move = TextIO.stringToMove(pos, "Nxe5");
    pos.makeMove(move, ui);
    assertEquals(0, pos.halfMoveClock); // Capture move resets 50 move counter
    assertEquals(7, pos.fullMoveCounter);
    pos.unMakeMove(move, ui);

    move = TextIO.stringToMove(pos, "cxb6");
    pos.makeMove(move, ui);
    assertEquals(0, pos.halfMoveClock); // EP capture move resets 50 move counter
    assertEquals(7, pos.fullMoveCounter);
    pos.unMakeMove(move, ui);

    move = TextIO.stringToMove(pos, "Kf1");
    pos.makeMove(move, ui);
    assertEquals(1, pos.halfMoveClock); // Loss of castling rights does not reset 50 move counter
    assertEquals(7, pos.fullMoveCounter);
    pos.unMakeMove(move, ui);

    Move firstMove = TextIO.stringToMove(pos, "Nc3");
    UndoInfo firstUi = new UndoInfo();
    pos.makeMove(move, firstUi);
    move = TextIO.stringToMove(pos, "O-O");
    pos.makeMove(move, ui);
    assertEquals(2, pos.halfMoveClock);
    assertEquals(8, pos.fullMoveCounter); // Black move increases fullMoveCounter
    pos.unMakeMove(move, ui);
    pos.unMakeMove(firstMove, firstUi);

    fen = "8/8/8/4k3/8/8/2p5/5K2 b - - 47 68";
    pos = TextIO.readFEN(fen);
    move = TextIO.stringToMove(pos, "c1Q");
    pos.makeMove(move, ui);
    assertEquals(0, pos.halfMoveClock); // Pawn promotion resets 50 move counter
    assertEquals(69, pos.fullMoveCounter);
  }
 public void Write_Line(Integer_Template.Integer i) {
   TextIO.putln(((Std_Integer_Realiz.Integer) i).val);
 }