Merge branch 'experimental_state'

This commit is contained in:
2020-07-16 19:20:50 +02:00
165 changed files with 1716 additions and 185 deletions

View File

@@ -0,0 +1,8 @@
<component name="ArtifactManager">
<artifact type="jar" name="TicTacToe - MinMax:jar">
<output-path>$PROJECT_DIR$/out/artifacts/TicTacToe___MinMax_jar</output-path>
<root id="archive" name="TicTacToe - MinMax.jar">
<element id="module-output" name="TicTacToe - MinMax" />
</root>
</artifact>
</component>

View File

@@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="SuspiciousListRemoveInLoop" enabled="false" level="WARNING" enabled_by_default="false" />
</profile>
</component>

2
.idea/misc.xml generated
View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_14" default="true" project-jdk-name="14" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_14" default="true" project-jdk-name="14 (2)" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

3
.idea/modules.xml generated
View File

@@ -6,10 +6,11 @@
<module fileurl="file://$PROJECT_DIR$/BruitForce/BruitForce.iml" filepath="$PROJECT_DIR$/BruitForce/BruitForce.iml" /> <module fileurl="file://$PROJECT_DIR$/BruitForce/BruitForce.iml" filepath="$PROJECT_DIR$/BruitForce/BruitForce.iml" />
<module fileurl="file://$PROJECT_DIR$/CodingChallanges/CodingChallanges.iml" filepath="$PROJECT_DIR$/CodingChallanges/CodingChallanges.iml" /> <module fileurl="file://$PROJECT_DIR$/CodingChallanges/CodingChallanges.iml" filepath="$PROJECT_DIR$/CodingChallanges/CodingChallanges.iml" />
<module fileurl="file://$PROJECT_DIR$/DemoProjects/DemoProjects.iml" filepath="$PROJECT_DIR$/DemoProjects/DemoProjects.iml" /> <module fileurl="file://$PROJECT_DIR$/DemoProjects/DemoProjects.iml" filepath="$PROJECT_DIR$/DemoProjects/DemoProjects.iml" />
<module fileurl="file://$PROJECT_DIR$/Doodles/Doodles.iml" filepath="$PROJECT_DIR$/Doodles/Doodles.iml" />
<module fileurl="file://$PROJECT_DIR$/ImageToText/ImageToText.iml" filepath="$PROJECT_DIR$/ImageToText/ImageToText.iml" /> <module fileurl="file://$PROJECT_DIR$/ImageToText/ImageToText.iml" filepath="$PROJECT_DIR$/ImageToText/ImageToText.iml" />
<module fileurl="file://$PROJECT_DIR$/LanguageAnalysis/LanguageAnalysis.iml" filepath="$PROJECT_DIR$/LanguageAnalysis/LanguageAnalysis.iml" /> <module fileurl="file://$PROJECT_DIR$/LanguageAnalysis/LanguageAnalysis.iml" filepath="$PROJECT_DIR$/LanguageAnalysis/LanguageAnalysis.iml" />
<module fileurl="file://$PROJECT_DIR$/TOOLS/TOOLS.iml" filepath="$PROJECT_DIR$/TOOLS/TOOLS.iml" /> <module fileurl="file://$PROJECT_DIR$/TOOLS/TOOLS.iml" filepath="$PROJECT_DIR$/TOOLS/TOOLS.iml" />
<module fileurl="file://$PROJECT_DIR$/TicTacToe_MinMax/TicTacToe_MinMax.iml" filepath="$PROJECT_DIR$/TicTacToe_MinMax/TicTacToe_MinMax.iml" /> <module fileurl="file://$PROJECT_DIR$/TicTacToe - MinMax/TicTacToe - MinMax.iml" filepath="$PROJECT_DIR$/TicTacToe - MinMax/TicTacToe - MinMax.iml" />
</modules> </modules>
</component> </component>
</project> </project>

View File

@@ -1,3 +1,5 @@
package src;
import java.util.Scanner; import java.util.Scanner;
public class Operator { public class Operator {

View File

@@ -17,5 +17,15 @@
<SOURCES /> <SOURCES />
</library> </library>
</orderEntry> </orderEntry>
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component> </component>
</module> </module>

BIN
DemoProjects/res/laser.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@@ -0,0 +1,79 @@
package animation.swingTimer;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel
implements ActionListener {
private final int B_WIDTH = 700;
private final int B_HEIGHT = 700;
private final int INITIAL_X = -40;
private final int INITIAL_Y = -40;
private final int DELAY = 5;
private Image star;
private Timer timer;
private int x, y;
public Board() {
initBoard();
}
private void loadImage() {
ImageIcon ii = new ImageIcon("/home/bitecoding/Pictures/star.png");
star = ii.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT);
}
private void initBoard() {
setBackground(Color.BLACK);
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
loadImage();
x = INITIAL_X;
y = INITIAL_Y;
timer = new Timer(DELAY, this);
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawStar(g);
}
private void drawStar(Graphics g) {
g.drawImage(star, x, y, this);
Toolkit.getDefaultToolkit().sync();
}
@Override
public void actionPerformed(ActionEvent e) {
x += 10;
y += 10;
if (y > B_HEIGHT) {
y = INITIAL_Y;
x = INITIAL_X;
}
repaint();
}
}

View File

@@ -0,0 +1,32 @@
package animation.swingTimer;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class SwingTimer extends JFrame {
public SwingTimer() {
initUI();
}
private void initUI() {
add(new Board());
setResizable(false);
pack();
setTitle("Star");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
SwingTimer ex = new SwingTimer();
ex.setVisible(true);
});
}
}

View File

@@ -0,0 +1,112 @@
package animation.thread;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Board extends JPanel
implements Runnable {
private final int B_WIDTH = 350;
private final int B_HEIGHT = 350;
private final int INITIAL_X = -40;
private final int INITIAL_Y = -40;
private final int DELAY = 25;
private Image star;
private Thread animator;
private int x, y;
public Board() {
initBoard();
}
private void loadImage() {
ImageIcon ii = new ImageIcon("/home/bitecoding/Pictures/star.png");
star = ii.getImage().getScaledInstance(50,50, Image.SCALE_DEFAULT);
}
private void initBoard() {
setBackground(Color.BLACK);
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
loadImage();
x = INITIAL_X;
y = INITIAL_Y;
}
@Override
public void addNotify() {
super.addNotify();
animator = new Thread(this);
animator.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawStar(g);
}
private void drawStar(Graphics g) {
g.drawImage(star, x, y, this);
Toolkit.getDefaultToolkit().sync();
}
private void cycle() {
x += 1;
y += 1;
if (y > B_HEIGHT) {
y = INITIAL_Y;
x = INITIAL_X;
}
}
@Override
public void run() {
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
while (true) {
cycle();
repaint();
timeDiff = System.currentTimeMillis() - beforeTime;
sleep = DELAY - timeDiff;
if (sleep < 0) {
sleep = 2;
}
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
String msg = String.format("Thread interrupted: %s", e.getMessage());
JOptionPane.showMessageDialog(this, msg, "Error",
JOptionPane.ERROR_MESSAGE);
}
beforeTime = System.currentTimeMillis();
}
}
}

View File

@@ -0,0 +1,32 @@
package animation.thread;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class ThreadAnimation extends JFrame {
public ThreadAnimation() {
initUI();
}
private void initUI() {
add(new Board());
setResizable(false);
pack();
setTitle("Star");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame ex = new ThreadAnimation();
ex.setVisible(true);
});
}
}

View File

@@ -0,0 +1,81 @@
package animation.utilityTimer;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Board extends JPanel {
private final int B_WIDTH = 350;
private final int B_HEIGHT = 350;
private final int INITIAL_X = -40;
private final int INITIAL_Y = -40;
private final int INITIAL_DELAY = 0;
private final int PERIOD_INTERVAL = 25;
private Image star;
private Timer timer;
private int x, y;
public Board() {
initBoard();
}
private void loadImage() {
ImageIcon ii = new ImageIcon("/home/bitecoding/Pictures/star.png");
star = ii.getImage().getScaledInstance(50,50,Image.SCALE_DEFAULT);
}
private void initBoard() {
setBackground(Color.BLACK);
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
loadImage();
x = INITIAL_X;
y = INITIAL_Y;
timer = new Timer();
timer.scheduleAtFixedRate(new ScheduleTask(),
INITIAL_DELAY, PERIOD_INTERVAL);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawStar(g);
}
private void drawStar(Graphics g) {
g.drawImage(star, x, y, this);
Toolkit.getDefaultToolkit().sync();
}
private class ScheduleTask extends TimerTask {
@Override
public void run() {
x += 1;
y += 1;
if (y > B_HEIGHT) {
y = INITIAL_Y;
x = INITIAL_X;
}
repaint();
}
}
}

View File

@@ -0,0 +1,32 @@
package animation.utilityTimer;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class UtilityTimer extends JFrame {
public UtilityTimer() {
initUI();
}
private void initUI() {
add(new Board());
setResizable(false);
pack();
setTitle("Star");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame ex = new UtilityTimer();
ex.setVisible(true);
});
}
}

View File

@@ -1,4 +1,4 @@
package painting; package painting.SwingPaintDemo;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.JFrame; import javax.swing.JFrame;

View File

@@ -1,4 +1,4 @@
package painting; package painting.SwingPaintDemo;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.JFrame; import javax.swing.JFrame;

View File

@@ -1,4 +1,4 @@
package painting; package painting.SwingPaintDemo;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.JFrame; import javax.swing.JFrame;
@@ -8,10 +8,7 @@ import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;
public class SwingPaintDemo3 { public class SwingPaintDemo3 {

View File

@@ -1,4 +1,4 @@
package painting; package painting.SwingPaintDemo;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.JFrame; import javax.swing.JFrame;

View File

@@ -0,0 +1,50 @@
package painting.donut;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
public class Board extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawDonut(g);
}
private void drawDonut(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
RenderingHints rh
= new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
Dimension size = getSize();
double w = size.getWidth();
double h = size.getHeight();
Ellipse2D e = new Ellipse2D.Double(0, 0, 80, 130);
g2d.setStroke(new BasicStroke(1));
g2d.setColor(Color.gray);
for (double deg = 0; deg < 360; deg += 10) {
AffineTransform at
= AffineTransform.getTranslateInstance(w/2, h/2);
at.rotate(Math.toRadians(deg));
g2d.draw(at.createTransformedShape(e));
}
}
}

View File

@@ -0,0 +1,31 @@
package painting.donut;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Donut extends JFrame {
public Donut() {
initUI();
}
private void initUI() {
add(new Board());
setSize(330, 330);
setTitle("Donut");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Donut ex = new Donut();
ex.setVisible(true);
});
}
}

View File

@@ -0,0 +1,38 @@
package painting.image;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Board extends JPanel {
private Image bardejov;
public Board() {
initBoard();
}
private void initBoard() {
loadImage();
int w = bardejov.getWidth(this);
int h = bardejov.getHeight(this);
setPreferredSize(new Dimension(w, h));
}
private void loadImage() {
ImageIcon ii = new ImageIcon("/home/bitecoding/Pictures/1182657.jpg");
bardejov = ii.getImage();
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(bardejov, 0, 0, null);
}
}

View File

@@ -0,0 +1,27 @@
package painting.image;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Image extends JFrame {
public Image() {
initUI();
}
private void initUI() {
add(new Board());
pack();
setTitle("Bardejov");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Image ex = new Image();
ex.setVisible(true);
});
}
}

View File

@@ -0,0 +1,100 @@
package sprites.shootingMissles;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;
public class Board extends JPanel implements ActionListener {
private final int ICRAFT_X = 40;
private final int ICRAFT_Y = 60;
private final int DELAY = 10;
private Timer timer;
private SpaceShip spaceShip;
public Board() {
initBoard();
}
private void initBoard() {
addKeyListener(new TAdapter());
setBackground(Color.BLACK);
setFocusable(true);
spaceShip = new SpaceShip(ICRAFT_X, ICRAFT_Y);
timer = new Timer(DELAY, this);
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
Toolkit.getDefaultToolkit().sync();
}
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(spaceShip.getImage(), spaceShip.getX(),
spaceShip.getY(), this);
List<Missile> missiles = spaceShip.getMissiles();
for (Missile missile : missiles) {
g2d.drawImage(missile.getImage(), missile.getX(),
missile.getY(), this);
}
}
@Override
public void actionPerformed(ActionEvent e) {
updateMissiles();
updateSpaceShip();
repaint();
}
private void updateMissiles() {
List<Missile> missiles = spaceShip.getMissiles();
for (int i = 0; i < missiles.size(); i++) {
Missile missile = missiles.get(i);
if (missile.isVisible()) {
missile.move();
} else {
missiles.remove(i);
}
}
}
private void updateSpaceShip() {
spaceShip.move();
}
private class TAdapter extends KeyAdapter {
@Override
public void keyReleased(KeyEvent e) {
spaceShip.keyReleased(e);
}
@Override
public void keyPressed(KeyEvent e) {
spaceShip.keyPressed(e);
}
}
}

View File

@@ -0,0 +1,32 @@
package sprites.shootingMissles;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Executor extends JFrame {
public Executor() {
initUI();
}
private void initUI() {
add(new Board());
setSize(400, 300);
setResizable(false);
setTitle("Shooting missiles");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
Executor ex = new Executor();
ex.setVisible(true);
});
}
}

View File

@@ -0,0 +1,26 @@
package sprites.shootingMissles;
public class Missile extends Sprite {
private final int MISSILE_SPEED = 2;
private final int FRAME_WIDTH = 400;
public Missile(int x, int y) {
super(x, y);
initMissile();
}
private void initMissile() {
loadImage("DemoProjects/res/laser.png",100,10);
}
public void move() {
x += MISSILE_SPEED;
if (x > FRAME_WIDTH) {
visible = false;
}
}
}

View File

@@ -0,0 +1,84 @@
package sprites.shootingMissles;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
public class SpaceShip extends Sprite {
private int dx;
private int dy;
private List<Missile> missiles;
public SpaceShip(int x, int y) {
super(x, y);
initSpaceShip();
}
private void initSpaceShip() {
missiles = new ArrayList<>();
loadImage("DemoProjects/res/spaceship.png",50,50);
}
public void move() {
x += dx;
y += dy;
}
public List<Missile> getMissiles() {
return missiles;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE) {
fire();
}
if (key == KeyEvent.VK_LEFT) {
dx = -1;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 1;
}
if (key == KeyEvent.VK_UP) {
dy = -1;
}
if (key == KeyEvent.VK_DOWN) {
dy = 1;
}
}
public void fire() {
missiles.add(new Missile(x - width/2, y + height/2 - 5));
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = 0;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}
if (key == KeyEvent.VK_UP) {
dy = 0;
}
if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}
}

View File

@@ -0,0 +1,53 @@
package sprites.shootingMissles;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class Sprite {
protected int x;
protected int y;
protected int width;
protected int height;
protected boolean visible;
protected String path;
protected Image image;
public Sprite(int x, int y) {
this.x = x;
this.y = y;
visible = true;
}
protected void loadImage(String imageName, int width, int height) {
path = imageName;
ImageIcon ii = new ImageIcon(imageName);
image = ii.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT);
this.width = width;
this.height = height;
}
public Image getImage() {
return image;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isVisible() {
return visible;
}
public void setVisible(Boolean visible) {
this.visible = visible;
}
}

View File

@@ -0,0 +1,79 @@
package sprites.spaceship;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener {
private Timer timer;
private SpaceShip spaceShip;
private final int DELAY = 10;
public Board() {
initBoard();
}
private void initBoard() {
addKeyListener(new TAdapter());
setBackground(Color.black);
setFocusable(true);
spaceShip = new SpaceShip();
timer = new Timer(DELAY, this);
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
Toolkit.getDefaultToolkit().sync();
}
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(spaceShip.getImage(), spaceShip.getX(),
spaceShip.getY(), this);
}
@Override
public void actionPerformed(ActionEvent e) {
step();
}
private void step() {
spaceShip.move();
repaint();
}
private class TAdapter extends KeyAdapter {
@Override
public void keyReleased(KeyEvent e) {
spaceShip.keyReleased(e);
}
@Override
public void keyPressed(KeyEvent e) {
spaceShip.keyPressed(e);
}
}
}

View File

@@ -0,0 +1,32 @@
package sprites.spaceship;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class MovingSprite extends JFrame {
public MovingSprite() {
initUI();
}
private void initUI() {
add(new Board());
setTitle("Moving sprite");
setSize(400, 300);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
MovingSprite ex = new MovingSprite();
ex.setVisible(true);
});
}
}

View File

@@ -0,0 +1,105 @@
package sprites.spaceship;
import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
public class SpaceShip {
private int dx;
private int dy;
private int x = 40;
private int y = 60;
private int w;
private int h;
private Image image;
public SpaceShip() {
loadImage();
}
private void loadImage() {
ImageIcon ii = new ImageIcon("/home/bitecoding/Pictures/spaceship.png");
image = ii.getImage().getScaledInstance(50,50,1);
w = image.getWidth(null);
h = image.getHeight(null);
}
public void move() {
x += dx;
y += dy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return w;
}
public int getHeight() {
return h;
}
public Image getImage() {
return image;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_A:
dx = -2;
break;
case KeyEvent.VK_D:
dx = 2;
break;
case KeyEvent.VK_W:
dy = -2;
break;
case KeyEvent.VK_S:
dy = 2;
break;
default:
break;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_A:
dx = 0;
break;
case KeyEvent.VK_D:
dx = 0;
break;
case KeyEvent.VK_W:
dy = 0;
break;
case KeyEvent.VK_S:
dy = 0;
break;
default:
break;
}
}
}

23
Doodles/src/Template.java Normal file
View File

@@ -0,0 +1,23 @@
import java.util.Arrays;
public class Template {
/*
1 2 3 4 5
12 13 14 15 6
11 10 9 8 7
*/
public static void main(String[] args) {
int num = 1000;
for (int j = 0; j < 50; j++) {
for (int i = 0; i < 59; i++) {
System.out.print(num+" ");
num++;
}
System.out.println();
}
}
}

View File

@@ -7,6 +7,7 @@
<sourceFolder url="file://$MODULE_DIR$/fileReader" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/fileReader" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/fileWriter" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/fileWriter" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/time" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/time" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/dataStructure" isTestSource="false" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />

View File

@@ -0,0 +1,225 @@
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class BinaryTree {
Node root;
public void add(int value) {
root = addRecursive(root, value);
}
private Node addRecursive(Node current, int value) {
if (current == null) {
return new Node(value);
}
if (value < current.value) {
current.left = addRecursive(current.left, value);
} else if (value > current.value) {
current.right = addRecursive(current.right, value);
}
return current;
}
public boolean isEmpty() {
return root == null;
}
public int getSize() {
return getSizeRecursive(root);
}
private int getSizeRecursive(Node current) {
return current == null ? 0 : getSizeRecursive(current.left) + 1 + getSizeRecursive(current.right);
}
public boolean containsNode(int value) {
return containsNodeRecursive(root, value);
}
private boolean containsNodeRecursive(Node current, int value) {
if (current == null) {
return false;
}
if (value == current.value) {
return true;
}
return value < current.value
? containsNodeRecursive(current.left, value)
: containsNodeRecursive(current.right, value);
}
public void delete(int value) {
root = deleteRecursive(root, value);
}
private Node deleteRecursive(Node current, int value) {
if (current == null) {
return null;
}
if (value == current.value) {
// Case 1: no children
if (current.left == null && current.right == null) {
return null;
}
// Case 2: only 1 child
if (current.right == null) {
return current.left;
}
if (current.left == null) {
return current.right;
}
// Case 3: 2 children
int smallestValue = findSmallestValue(current.right);
current.value = smallestValue;
current.right = deleteRecursive(current.right, smallestValue);
return current;
}
if (value < current.value) {
current.left = deleteRecursive(current.left, value);
return current;
}
current.right = deleteRecursive(current.right, value);
return current;
}
private int findSmallestValue(Node root) {
return root.left == null ? root.value : findSmallestValue(root.left);
}
public void traverseInOrder(Node node) {
if (node != null) {
traverseInOrder(node.left);
visit(node.value);
traverseInOrder(node.right);
}
}
public void traversePreOrder(Node node) {
if (node != null) {
visit(node.value);
traversePreOrder(node.left);
traversePreOrder(node.right);
}
}
public void traversePostOrder(Node node) {
if (node != null) {
traversePostOrder(node.left);
traversePostOrder(node.right);
visit(node.value);
}
}
public void traverseLevelOrder() {
if (root == null) {
return;
}
Queue<Node> nodes = new LinkedList<>();
nodes.add(root);
while (!nodes.isEmpty()) {
Node node = nodes.remove();
System.out.print(" " + node.value);
if (node.left != null) {
nodes.add(node.left);
}
if (node.right != null) {
nodes.add(node.right);
}
}
}
public void traverseInOrderWithoutRecursion() {
Stack<Node> stack = new Stack<Node>();
Node current = root;
stack.push(root);
while(! stack.isEmpty()) {
while(current.left != null) {
current = current.left;
stack.push(current);
}
current = stack.pop();
visit(current.value);
if(current.right != null) {
current = current.right;
stack.push(current);
}
}
}
public void traversePreOrderWithoutRecursion() {
Stack<Node> stack = new Stack<Node>();
Node current = root;
stack.push(root);
while(! stack.isEmpty()) {
current = stack.pop();
visit(current.value);
if(current.right != null)
stack.push(current.right);
if(current.left != null)
stack.push(current.left);
}
}
public void traversePostOrderWithoutRecursion() {
Stack<Node> stack = new Stack<Node>();
Node prev = root;
Node current = root;
stack.push(root);
while (!stack.isEmpty()) {
current = stack.peek();
boolean hasChild = (current.left != null || current.right != null);
boolean isPrevLastChild = (prev == current.right || (prev == current.left && current.right == null));
if (!hasChild || isPrevLastChild) {
current = stack.pop();
visit(current.value);
prev = current;
} else {
if (current.right != null) {
stack.push(current.right);
}
if (current.left != null) {
stack.push(current.left);
}
}
}
}
private void visit(int value) {
System.out.print(" " + value);
}
class Node {
int value;
Node left;
Node right;
Node(int value) {
this.value = value;
right = null;
left = null;
}
}
}

View File

@@ -0,0 +1,12 @@
<?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" />
<orderEntry type="module" module-name="TOOLS" exported="" />
</component>
</module>

View File

@@ -0,0 +1,147 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
public class Board extends JPanel implements ActionListener {
private final int B_WIDTH = 900;
private final int B_HEIGHT = 900;
private final int TILE_X = 300;
private final int TILE_Y = 300;
private final int DELAY = 50;
private boolean ended = false;
private boolean gameWon = false;
private boolean initialized = false;
int[] oldPlayfield;
private Timer timer;
private Game game;
private Painter painter;
public Board(){
initBoard();
}
private void initBoard(){
painter = new Painter(B_WIDTH,B_HEIGHT);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
int column = e.getX()/TILE_X;
int row = e.getY()/TILE_Y;
game.place(column * 3 + row, 1);
}
});
setBackground(Color.BLACK);
setFocusable(true);
setPreferredSize(new Dimension(B_WIDTH,B_HEIGHT));
initGame();
}
private void initGame(){
game = new Game();
oldPlayfield = game.getPlayfield().clone();
timer = new Timer(DELAY, this);
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
painter.paintGrid(g);
updateBoard(g);
}
private void updateBoard(Graphics g){
int actions = 0;
for (int column = 0; column < 3; column++) {
for (int row = 0; row < 3; row++) {
if (game.getPlayfield()[actions] == 1) {
painter.drawX(g, column, row);
} else if (game.getPlayfield()[actions] == -1) {
painter.drawO(g, column, row);
}
actions++;
}
}
if (ended) {
painter.paintWinnerLine(g);
}
}
public void resetBoard(){
for (int i = 0; i < game.getPlayfield().length; i++){
game.setPlayfield(i, 0);
}
timer.start();
oldPlayfield = game.getPlayfield().clone();
game.setTurnTaken(false);
ended = false;
repaint();
}
public void setWinningLine(){
painter.setWinningX1(game.getWinningX1());
painter.setWinningY1(game.getWinningY1());
painter.setWinningX2(game.getWinningX2());
painter.setWinningY2(game.getWinningY2());
}
//game controlling method
@Override
public void actionPerformed(ActionEvent e) {
Thread actionThread = new Thread(){
@Override
public void run() {
//check if game state evaluation needs to be done
if (isChanged(oldPlayfield)) {
gameWon = game.checkWin();
//repaint board if not won
if (!gameWon) {
repaint();
oldPlayfield = game.getPlayfield().clone();
}
//stop timer if game won
else if (gameWon || game.emptyTiles() == 0) {
ended = true;
setWinningLine();
repaint();
timer.stop();
try {
Thread.sleep(1000);
int n = JOptionPane.showConfirmDialog(null, "Do you want to play again?");
if (n == 0){
resetBoard();
} else {
System.exit(0);
}
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
}
//check if computer needs to take a turn
if (game.isTurnTaken()){
game.setTurnTaken(false);
game.computersTurn();
}
}
};
actionThread.start();
}
private boolean isChanged(int[] playfield){
return !Arrays.equals(game.getPlayfield(), playfield);
}
}

View File

@@ -0,0 +1,26 @@
import javax.swing.*;
public class Executor extends JFrame {
public Executor(){
initUI();
}
private void initUI(){
Board board = new Board();
setTitle("TicTacToe - MinMax");
add(board);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Executor exc = new Executor();
exc.setVisible(true);
}
});
}
}

View File

@@ -0,0 +1,116 @@
import javax.swing.*;
public class Game {
private int[] playfield;
private boolean turnTaken = false;
private int winningX1,winningY1,winningX2,winningY2;
public Game(){
playfield = new int[9];
}
public void place(int position, int player){
if (playfield[position] == 0){
playfield[position] = player;
if (player == 1) {
turnTaken = true;
}
} else {
JOptionPane.showInternalMessageDialog(null,"Tile is already taken");
}
}
public void computersTurn(){
boolean isPlaced = false;
try {
Thread.sleep(750);
} catch (InterruptedException e) {
e.printStackTrace();
}
while(!isPlaced){
int random = (int) (Math.random() * 9);
// if field is free
if (playfield[random] == 0) {
place(random, -1);
isPlaced = true;
}
}
}
public boolean checkWin() {
//only check if winning is possible
if (emptyTiles() < 5) {
for (int i = 0; i < 3; i++) {
//horizontal
if ((playfield[i] == playfield[i + 3] && playfield[i] != 0) && (playfield[i] == playfield[i + 6])) {
winningX1 = 75;
winningX2 = 825;
winningY1 = winningY2 = i * 300 + 150;
return true;
}
//vertical
else if ((playfield[i * 3] == playfield[i * 3 + 1] && playfield[i * 3] != 0) && (playfield[i * 3] == playfield[i * 3 + 2])) {
winningY1 = 75;
winningY2 = 825;
winningX1 = winningX2 = i * 300 + 150;
return true;
}
}
//diagonal
if ((playfield[2] == playfield[4] && playfield[2] != 0) && (playfield[2] == playfield[6])){
winningX2 = winningY1 = 75;
winningX1 = winningY2 = 825;
return true;
} else if ((playfield[0] == playfield[4] && playfield[0] != 0) && (playfield[0] == playfield[8])){
winningX1 = winningY1 = 75;
winningX2 = winningY2 = 825;
return true;
}
}
return false;
}
public int emptyTiles(){
int n = 9;
for (int i = 0; i < playfield.length; i++){
if (playfield[i] != 0){
n -= 1;
}
}
System.out.println(n);
return n;
}
public boolean isTurnTaken() {
return turnTaken;
}
public void setTurnTaken(boolean turnTaken) {
this.turnTaken = turnTaken;
}
public void setPlayfield(int position, int value) {
playfield[position] = value;
}
public int[] getPlayfield() {
return playfield;
}
public int getWinningX1() {
return winningX1;
}
public int getWinningX2() {
return winningX2;
}
public int getWinningY1() {
return winningY1;
}
public int getWinningY2() {
return winningY2;
}
}

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Executor

View File

@@ -0,0 +1,73 @@
import java.awt.*;
public class Painter {
private final int TILE_X;
private final int TILE_Y;
private int winningX1, winningY1, winningX2, winningY2;
public Painter(int boardWidth, int boardHeight){
TILE_X = boardWidth/3;
TILE_Y = boardHeight/3;
}
public void drawX(Graphics g, int column, int row) {
Graphics2D g2d = (Graphics2D) g;
int nextColumn = column + 1;
int nextRow = row + 1;
int x1 = column * TILE_X + 25;
int x2 = nextColumn * TILE_Y - 25;
int y1 = row * TILE_X + 25;
int y2 = nextRow * TILE_Y - 25;
g2d.setColor(Color.WHITE);
g2d.setStroke(new BasicStroke(5));
g2d.drawLine(x1, y1, x2, y2);
g2d.drawLine(x1, y2, x2, y1);
}
public void drawO(Graphics g, int column, int row){
int x = column * TILE_X + 25;
int y = row * TILE_Y + 25;
g.drawOval(x,y,250,250);
}
public void paintGrid(Graphics g){
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.WHITE);
g2d.setStroke(new BasicStroke(10));
//horizontal
for (int i = 1; i < 3; i++) {
g2d.drawLine(0, TILE_Y*i, TILE_X*3, TILE_Y*i);
}
//vertical
for (int i = 1; i < 3; i++){
g2d.drawLine(TILE_X*i, 0, TILE_X*i, TILE_Y*3);
}
}
public void paintWinnerLine(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.setStroke(new BasicStroke(40));
g2d.drawLine(winningX1, winningY1, winningX2, winningY2);
}
public void setWinningX1(int winningX1) {
this.winningX1 = winningX1;
}
public void setWinningX2(int winningX2) {
this.winningX2 = winningX2;
}
public void setWinningY1(int winningY1) {
this.winningY1 = winningY1;
}
public void setWinningY2(int winningY2) {
this.winningY2 = winningY2;
}
}

View File

@@ -0,0 +1,9 @@
Variables:
player, board, search_depth
Data_structure:
BinaryTree
Loops:
recursion
for-loop

View File

@@ -1,41 +0,0 @@
import javax.swing.*;
public class Game {
public void place(int player, int position, int[] playfield){
if (playfield[position] == -1){
playfield[position] = player;
} else {
JOptionPane.showConfirmDialog(null,"Tile is already taken");
}
}
private boolean checkWin(int[] playfield){
for (int i = 0; i < 3; i++){
if ((playfield[i] == playfield[i+3]) && (playfield[i] == playfield[i+6])){
System.out.println("vertical");
return true;
} else if ((playfield[i*3] == playfield[i*3+1]) && (playfield[i*3] == playfield[i*3+2])){
System.out.println("horizontal");
return true;
}
}
return (playfield[2] == playfield[4]) && (playfield[2] == playfield[6]) || (playfield[0] == playfield[4]) && (playfield[0] == playfield[8]);
}
public static void main(String[] args) {
Game game = new Game();
Render render = new Render();
int[] playfield = {1,0,-1,1,1,0,-1,0,1};
SwingUtilities.invokeLater(new Runnable() {
public void run() {
render.createUI(playfield);
}
});
System.out.println(game.checkWin(playfield));
}
}

View File

@@ -1,135 +0,0 @@
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
public class Render {
public static void createUI(int[] playfield){
JFrame f = new JFrame("Swing Paint Demo");
MyPanel myPanel = new MyPanel(playfield);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(myPanel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class MyPanel extends JPanel {
Grid grid;
GameCharacters gameCharacters;
public MyPanel() {
setBorder(BorderFactory.createLineBorder(Color.BLACK));
grid = new Grid();
}
public MyPanel(int[] playfield){
setBorder(BorderFactory.createLineBorder(Color.BLACK));
grid = new Grid();
gameCharacters = new GameCharacters(playfield);
}
public Dimension getPreferredSize() {
return new Dimension(900,900);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
grid.paintGrid(g);
gameCharacters.paintGameCharacters(g);
}
}
class Grid{
private final int TILE_X = 300;
private final int TILE_Y = 300;
public void paintGrid(Graphics g){
g.setColor(Color.BLACK);
//horizontal
for (int i = 1; i < 3; i++) {
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(5));
g2d.drawLine(0, TILE_Y*i, TILE_X*3, TILE_Y*i);
}
//vertical
for (int i = 1; i < 3; i++){
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(5));
g2d.drawLine(TILE_X*i, 0, TILE_X*i, TILE_Y*3);
}
}
}
class GameCharacters {
CharacterX characterX = new CharacterX();
CharacterO characterO = new CharacterO();
private final int TILE_X = 300;
private final int TILE_Y = 300;
int[] playfield;
public GameCharacters(int[] playfield) {
this.playfield = playfield;
}
public void paintGameCharacters(Graphics g) {
int actions = 0;
for (int column = 0; column < 3; column++) {
for (int row = 0; row < 3; row++) {
if (playfield[actions] == 1) {
characterX.setX(TILE_X * row);
characterX.setY(TILE_Y * column);
characterX.paintX(g);
} else if (playfield[actions] == -1) {
characterO.setX(TILE_X * row);
characterO.setY(TILE_Y * column);
characterO.paintO(g);
}
actions++;
}
}
}
class CharacterX {
private int x;
private int y;
public void paintX(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(10));
g2d.drawLine(x+25, y+25, x+275, y+275);
g2d.drawLine(x+25, y +275, x+275, y+25);
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
class CharacterO {
private int x;
private int y;
public void paintO(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(10));
g2d.drawOval(x + 25, y + 25, 250, 250);
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
}

Binary file not shown.

View File

@@ -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$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More