|
| 1 | +package com.diffblue.java_test; |
| 2 | + |
| 3 | +public class Additions |
| 4 | +{ |
| 5 | + /* checks if an array represents a valid tic-tac-toe |
| 6 | + * position. Encoding of input: |
| 7 | + * 1: player X field |
| 8 | + * 2: player O field |
| 9 | + * 0: empty position |
| 10 | + * |
| 11 | + * return values: |
| 12 | + * -1: not a correct position |
| 13 | + * 0: nobody has won |
| 14 | + * 1: player X has won |
| 15 | + * 2: player O has won |
| 16 | + */ |
| 17 | + byte checkTicTacToePosition(byte[] a) |
| 18 | + { |
| 19 | + //first check number of rows |
| 20 | + if (a.length != 9) |
| 21 | + return -1; |
| 22 | + |
| 23 | + //next check number of moves each player made |
| 24 | + byte diff = 0; |
| 25 | + for (byte i=0; i<9; i++) { |
| 26 | + if (a[i] == 1) |
| 27 | + diff++; |
| 28 | + else if (a[i] == 2) |
| 29 | + diff--; |
| 30 | + else if (a[i] != 0) |
| 31 | + return -1; |
| 32 | + } |
| 33 | + |
| 34 | + if (diff > 1 || diff < -1) |
| 35 | + return -1; |
| 36 | + |
| 37 | + //Now we know we have a valid position |
| 38 | + |
| 39 | + //Check if a row and column is winning |
| 40 | + for (byte i=0; i<3; i++) { |
| 41 | + if (a[i] == a[3+i] && a[i] == a[6+i]) { |
| 42 | + if(a[i] == 1) |
| 43 | + return 1; |
| 44 | + else if (a[i] == 2) |
| 45 | + return 2; |
| 46 | + } |
| 47 | + |
| 48 | + if (a[3*i] == a[3*i + 1] && a[3*i] == a[3*i + 2]) { |
| 49 | + if(a[3*i] == 1) |
| 50 | + return 1; |
| 51 | + else if (a[3*i] == 2) |
| 52 | + return 2; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + //Check if the top-left bottom-right diagonal is winning |
| 57 | + if (a[0] == a[4] && a[0] == a[8]) { |
| 58 | + if(a[0] == 1) |
| 59 | + return 1; |
| 60 | + else if (a[0] == 2) |
| 61 | + return 2; |
| 62 | + } |
| 63 | + |
| 64 | + //Check if the top-right bottom-left diagonal is winning |
| 65 | + if (a[2] == a[4] && a[2] == a[6]) { |
| 66 | + if(a[2] == 1) |
| 67 | + return 1; |
| 68 | + else if (a[2] == 2) |
| 69 | + return 2; |
| 70 | + } |
| 71 | + |
| 72 | + //nobody won yet |
| 73 | + return 0; |
| 74 | + } |
| 75 | + |
| 76 | + /* |
| 77 | + * checks if an array contains a sequence |
| 78 | + * [... '<','h','t','m','l' ...] |
| 79 | + */ |
| 80 | + public boolean containsUnsafeSequence(char[] ca){ |
| 81 | + int state = 0; |
| 82 | + for(int i=0; i<ca.length; i++) { |
| 83 | + switch(state) { |
| 84 | + case 0: if (ca[i]=='<') state=1; else state=0; break; |
| 85 | + case 1: if (ca[i]=='h') state=2; else state=0; break; |
| 86 | + case 2: if (ca[i]=='t') state=3; else state=0; break; |
| 87 | + case 3: if (ca[i]=='m') state=4; else state=0; break; |
| 88 | + case 4: if (ca[i]=='l') state=5; else state=0; break; |
| 89 | + default: ; |
| 90 | + } |
| 91 | + |
| 92 | + if (state == 5) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + /* applies bubblesort and checks |
| 101 | + * if the result is sorted |
| 102 | + */ |
| 103 | + public boolean bubbleSortWrapper(int[] a) { |
| 104 | + bubbleSort(a); |
| 105 | + |
| 106 | + System.out.println(java.util.Arrays.toString(a)); |
| 107 | + |
| 108 | + for (int i=0; i<a.length-1; i++) { |
| 109 | + if (a[i] > a[i+1]) |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + /* bubble sort -- with error */ |
| 117 | + public void bubbleSort(int [] a) |
| 118 | + { |
| 119 | + int j; |
| 120 | + boolean flag = true; |
| 121 | + int temp; |
| 122 | + |
| 123 | + while (flag) |
| 124 | + { |
| 125 | + flag=false; |
| 126 | + for(j=0; j<a.length-1; j++) |
| 127 | + { |
| 128 | + if (a[j] > a[j+1]+1) //hint: error is on this line. |
| 129 | + { |
| 130 | + temp = a[j]; |
| 131 | + a[j] = a[j+1]; |
| 132 | + a[j+1] = temp; |
| 133 | + flag = true; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments