Skip to content

Commit ebaa279

Browse files
committed
cohort 42.1 hw #9, #11 ext, lesson #12
1 parent a0af580 commit ebaa279

4 files changed

Lines changed: 235 additions & 5 deletions

File tree

BerlinAIT/Cohort42.1/src/extend/HomeWork9Ext.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
public class HomeWork9Ext {
1212
public static void main(String[] args) {
13-
String[] tokens = expressionToTokens("16-23-1+18");
13+
String[] tokens = expressionToTokens("16-2*3/2+18");
1414
System.out.println(Arrays.toString(tokens));
1515
System.out.println("= " + calculate(tokens));
1616
}
@@ -41,17 +41,40 @@ static String[] expressionToTokens(String exp) {
4141
}
4242

4343
static Integer calculate(String[] tokens) {
44+
String[] newTokens = new String[tokens.length];
45+
newTokens[0] = tokens[0];
46+
int length = 1;
47+
// calculation of higher priority operations '*','/'
4448
int result = Integer.valueOf(tokens[0]);
4549
for (int i = 1; i < tokens.length; i += 2) {
4650
switch (tokens[i]) {
51+
case "*":
52+
result *= Integer.valueOf(tokens[i + 1]);
53+
newTokens[length - 1] = String.valueOf(result);
54+
break;
55+
case "/":
56+
result /= Integer.valueOf(tokens[i + 1]);
57+
newTokens[length - 1] = String.valueOf(result);
58+
break;
59+
default:
60+
newTokens[length] = tokens[i];
61+
newTokens[length + 1] = tokens[i + 1];
62+
result = Integer.valueOf(tokens[i + 1]);
63+
length += 2;
64+
}
65+
}
66+
// calculation of low priority operations '+', '-'
67+
result = Integer.valueOf(newTokens[0]);
68+
for (int i = 1; i < length; i += 2) {
69+
switch (newTokens[i]) {
4770
case "+":
48-
result += Integer.valueOf(tokens[i + 1]);
71+
result += Integer.valueOf(newTokens[i + 1]);
4972
break;
5073
case "-":
51-
result -= Integer.valueOf(tokens[i + 1]);
74+
result -= Integer.valueOf(newTokens[i + 1]);
5275
break;
5376
default:
54-
System.out.println("Undefined operation: " + tokens[i]);
77+
System.out.println("Undefined operation: " + newTokens[i]);
5578
return null;
5679
}
5780
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package lesson11;
2+
3+
import java.util.Random;
4+
import java.util.Scanner;
5+
6+
/**
7+
* AIT-TR, cohort 42.1, Java Basic, hw #11
8+
*
9+
* @author Sergey Iryupin
10+
* @version 09-Feb-24
11+
*/
12+
public class HomeWork11 {
13+
static char[][] table = new char[3][3];
14+
static Scanner scanner = new Scanner(System.in);
15+
static Random random = new Random();
16+
static final char CHAR_EMPTY = '-';
17+
static final char CHAR_X = 'x';
18+
static final char CHAR_O = 'o';
19+
20+
public static void main(String[] args) {
21+
// init table (.)
22+
initTable();
23+
// main game loop
24+
while (true) {
25+
// human turn (x)
26+
turnHuman();
27+
// is human win? yes - game over
28+
if (isWin(CHAR_X)) {
29+
System.out.println("YOU WON!");
30+
break;
31+
}
32+
// is table fill? yes - game over
33+
if (isTableFill()) {
34+
System.out.println("Sorry, DRAW!");
35+
break;
36+
}
37+
// AI turn (o)
38+
turnAI();
39+
// is AI win? yes - game over
40+
if (isWin(CHAR_O)) {
41+
System.out.println("AI WON!");
42+
break;
43+
}
44+
// is table fill? yes - game over
45+
if (isTableFill()) {
46+
System.out.println("Sorry, DRAW");
47+
break;
48+
}
49+
// print table
50+
printTable();
51+
}
52+
// print table
53+
printTable();
54+
}
55+
56+
static void initTable() {
57+
for (int y = 0; y < 3; y++) {
58+
for (int x = 0; x < 3; x++) {
59+
table[y][x] = CHAR_EMPTY;
60+
}
61+
}
62+
}
63+
64+
static void printTable() {
65+
for (int y = 0; y < 3; y++) {
66+
for (int x = 0; x < 3; x++) {
67+
System.out.print(table[y][x] + " ");
68+
}
69+
System.out.println();
70+
}
71+
}
72+
73+
static void turnHuman() {
74+
int x, y;
75+
do {
76+
System.out.println("Enter x & y [0..2]:");
77+
x = scanner.nextInt();
78+
y = scanner.nextInt();
79+
} while(!isCellValid(x, y));
80+
table[y][x] = CHAR_X;
81+
}
82+
83+
static void turnAI() {
84+
int x, y;
85+
do {
86+
x = random.nextInt(3);
87+
y = random.nextInt(3);
88+
} while(!isCellValid(x, y));
89+
table[y][x] = CHAR_O;
90+
}
91+
92+
static boolean isCellValid(int x, int y) {
93+
if (x < 0 || y < 0 || x > 2 || y > 2) {
94+
return false;
95+
}
96+
return table[y][x] == CHAR_EMPTY;
97+
}
98+
99+
static boolean isWin(char chr) {
100+
for (int i = 0; i < 3; i++) {
101+
if ((table[i][0] == chr && table[i][1] == chr && table[i][2] == chr)
102+
|| (table[0][i] == chr && table[1][i] == chr && table[2][i] == chr)) {
103+
return true;
104+
}
105+
}
106+
// // by x
107+
// if (table[0][0] == chr && table[0][1] == chr && table[0][2] == chr) return true;
108+
// if (table[1][0] == chr && table[1][1] == chr && table[1][2] == chr) return true;
109+
// if (table[2][0] == chr && table[2][1] == chr && table[2][2] == chr) return true;
110+
// // by y
111+
// if (table[0][0] == chr && table[1][0] == chr && table[2][0] == chr) return true;
112+
// if (table[0][1] == chr && table[1][1] == chr && table[2][1] == chr) return true;
113+
// if (table[0][2] == chr && table[1][2] == chr && table[2][2] == chr) return true;
114+
// diagonals
115+
if ((table[0][0] == chr && table[1][1] == chr && table[2][2] == chr)
116+
|| (table[0][2] == chr && table[1][1] == chr && table[2][0] == chr)) {
117+
return true;
118+
}
119+
return false;
120+
}
121+
122+
static boolean isTableFill() {
123+
for (int y = 0; y < 3; y++) {
124+
for (int x = 0; x < 3; x++) {
125+
if (table[y][x] == CHAR_EMPTY) {
126+
return false;
127+
}
128+
}
129+
}
130+
return true;
131+
}
132+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package lesson12;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* AIT-TR, cohort 42.1, Java Basic, Lesson #12
7+
*
8+
* @version 9-Feb-24
9+
*/
10+
public class Lesson12 {
11+
public static void main(String[] args) {
12+
// linear search
13+
int[] array = {1, 8, 12, -4, 6, 3, 7, -3};
14+
int toSearch = -8;
15+
int idx = -1;
16+
for (int i = 0; i < array.length; i++) {
17+
if (array[i] == toSearch) {
18+
idx = i;
19+
break;
20+
}
21+
}
22+
System.out.println(idx);
23+
System.out.println(Arrays.toString(array));
24+
// sorting array
25+
selectionSort(array);
26+
System.out.println(Arrays.toString(array));
27+
int a = 5;
28+
addOne(a);
29+
System.out.println("a = " + a);
30+
}
31+
32+
static void addOne(int n) {
33+
n++;
34+
System.out.println("N = " + n);
35+
}
36+
37+
static void excangeSort(int[] a) {
38+
for (int i = 0; i < a.length - 1; i++) {
39+
for (int j = i + 1; j < a.length; j++) {
40+
if (a[i] > a[j]) {
41+
int tmp = a[i];
42+
a[i] = a[j];
43+
a[j] = tmp;
44+
}
45+
}
46+
}
47+
}
48+
49+
static void selectionSort(int[] a) {
50+
for (int i = 0; i < a.length - 1; i++) {
51+
int min = a[i + 1];
52+
int minIdx = i + 1;
53+
for (int j = i + 1; j < a.length; j++) {
54+
if (a[j] < min) {
55+
min = a[j];
56+
minIdx = j;
57+
}
58+
}
59+
if (a[i] > min) {
60+
a[minIdx] = a[i];
61+
a[i] = min;
62+
}
63+
}
64+
}
65+
}

BerlinAIT/Cohort42.1/src/lesson9/HomeWork9.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package lesson9;
22

33
import java.util.Arrays;
4+
import java.util.Random;
45

56
/**
67
* AIT-TR, cohort 42.1, Java Basic, hw #9
@@ -10,7 +11,7 @@
1011
*/
1112
public class HomeWork9 {
1213
public static void main(String[] args) {
13-
int[] array = {1, 2, 3, 4, 5, 6};
14+
int[] array = createRandomArray(6, 100);//{1, 2, 3, 4, 5, 6};
1415

1516
// task #1
1617
task1(5);
@@ -32,6 +33,15 @@ static void task1(int n) {
3233
}
3334
}
3435

36+
static int[] createRandomArray(int len, int bound) {
37+
int[] a = new int[len];
38+
Random random = new Random();
39+
for (int i = 0; i < a.length; i++) {
40+
a[i] = random.nextInt(bound);
41+
}
42+
return a;
43+
}
44+
3545
static void printArray(int[] a) {
3646
printArray(a, true);
3747
}

0 commit comments

Comments
 (0)