Skip to content

Commit

Permalink
11b - exam solution, stream api intro
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Benov committed Jan 10, 2024
1 parent 7c7321f commit 0b9f184
Show file tree
Hide file tree
Showing 18 changed files with 354 additions and 0 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
public abstract class Figure {
protected Game game;
protected String owner;
protected Game.Pos pos;

// private char[] display;
private char display;

public Figure(Game game, String owner, Game.Pos pos, char[] display) {
this.game = game;
this.owner = owner;
this.pos = pos;
// this.display = display;
this.display = owner == "white" ? display[0] : display[1];
}

public void move(String pos) {
Game.Pos newPos = Game.stringToPos(pos);
// 1. validate pos is in board
if(newPos.row < 0 || newPos.col < 0)
throw new IllegalArgumentException("Invalid position");
if(newPos.row > 7 || newPos.col > 7)
throw new IllegalArgumentException("Invalid position");
// 2. validate with canMove
if(!canMove(pos))
throw new IllegalArgumentException("Invalid position");
// 3. validate field is empty or owner by oponent
Figure targetCell = game.getAt(pos);
if(targetCell != null && targetCell.owner == owner)
throw new IllegalArgumentException("Cannot take own piece");
// 4. board[pos] = this
game.setAt(newPos.toString(), this);
// 5. board[this.pos] = null
game.setAt(this.pos.toString(), null);
// 6. this.pos = pos
this.pos = newPos;
}

protected abstract boolean canMove(String pos);

@Override
public String toString() {
return "" + display;
// if(owner == "white")
// return "" + display[0];
// else
// return "" + display[1];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
public class Game {
private Figure[][] board;

private King whiteKing = null;
private King blackKing = null;

public Game() {
board = new Figure[8][8];

board[0][0] = new Rook(this, "white", new Pos(0, 0));
board[3][0] = whiteKing = new King(this, "white", new Pos(0, 3));

board[0][7] = new Rook(this, "black", new Pos(7, 0));
board[3][7] = blackKing = new King(this, "black", new Pos(7, 3));
}

public Figure getAt(String pos) {
Pos newPos = stringToPos(pos);
return board[newPos.row][newPos.col];
}

public void setAt(String pos, Figure fig) {
Pos newPos = stringToPos(pos);
board[newPos.row][newPos.col] = fig;
}

public void moveFigure(String start, String end) {
Figure fig = getAt(start);
fig.move(end);
}

public void printBoard() {
for(int row = 0; row < 8; row++) {
for(int col = 0; col < 8; col++) {
Figure fig = getAt(new Pos(row, col).toString());
if(fig == null) System.out.print('-');
else System.out.print(fig);
}
System.out.println("");
}
}

public void checkForWinner() {
if(whiteKing.isMate()) {
System.out.println("Black wins");
} else if(blackKing.isMate()) {
System.out.println("White wins");
}
}

public static class Pos {
public int row;
public int col;

public Pos(int row, int col) {
this.row = row;
this.col = col;
}

@Override
public String toString() {
int r = 'A' + row;
int c = '1' + col;
return Character.toString(r) + Character.toString(c);
}
}

public static Pos stringToPos(String pos) {
int col = pos.charAt(0) - 'A';
int row = pos.charAt(1) - '1';

return new Pos(row, col);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class King extends Figure {
public King(Game game, String owner, Game.Pos pos) {
super(game, owner, pos, new char[] {'K', 'k'});
}

@Override
protected boolean canMove(String pos) {
return false;
}

public boolean isCheck() {
return false;
}

public boolean isMate() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
public class Main {
public static void main(String[] args) {
// String[][] arr = new String[10][5];
//// for(int i = 0; i < 5; i++) {
//// for(int j = 0; j < 10; j++) {
//// arr[i][j] = i + "-" + j;
//// }
//// }
// for(int x = 0; x < 10; x++) {
// for(int y = 0; y < 5; y++) {
// arr[x][y] = x + "-" + y;
// }
// }
//
// for(int row = 0; row < 5; row++) {
// for(int column = 0; column < 10; column++) {
// System.out.print(arr[row][column] + " ");
// }
// System.out.println("");
// }

Game g = new Game();
g.printBoard();

g.moveFigure("A1", "A4");
g.printBoard();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Rook extends Figure {
public Rook(Game game, String owner, Game.Pos pos) {
super(game, owner, pos, new char[] {'R', 'r'});
}

@Override
protected boolean canMove(String pos) {
return true;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.BaseStream;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
public static class User {
public String firstName;
public String lastName;
public int age;

public User(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}

@Override
public String toString() {
return "User{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", age=" + age +
'}';
}
}

public static void main(String[] args) {
int[] arr = {5, 3, 1, 2, 4};

BaseStream foo = Arrays.stream(arr);
Object[] res = Arrays.stream(arr)
.filter(a -> a > 3)
.sorted()
.map(a -> a * 2)
.mapToObj(a -> "foo-" + a)
.toArray();

System.out.println(Arrays.toString(res));

Random rand = new Random();
List<User> users = Stream.generate(() ->
new User("Name" + rand.nextInt(), "Name" + rand.nextInt(), rand.nextInt())
)
.limit(10)
.toList();

System.out.println(users);

List<User> res2 = users.stream()
// .sorted((a, b) -> a.firstName.compareTo(b.firstName))
.sorted(Comparator.comparing(a -> a.firstName))
.skip(10)
.limit(5)
.toList();

System.out.println(res2);

Optional<User> bar = users.stream()
.findFirst();
// bar.ifPresent(System.out::println);
if(!bar.isEmpty())
System.out.println(bar.get());

// users.stream()
// .collect(Collectors.toList())
// .collect(new Collector<User, Object, Object>() {
// @Override
// public Supplier<Object> supplier() {
// return null;
// }
//
// @Override
// public BiConsumer<Object, User> accumulator() {
// return null;
// }
//
// @Override
// public BinaryOperator<Object> combiner() {
// return null;
// }
//
// @Override
// public Function<Object, Object> finisher() {
// return null;
// }
//
// @Override
// public Set<Characteristics> characteristics() {
// return null;
// }
// })
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

0 comments on commit 0b9f184

Please sign in to comment.