learning 2d game programing

This commit is contained in:
2020-07-03 14:23:40 +02:00
parent aeb237b035
commit f2a938f93c
84 changed files with 1091 additions and 221 deletions

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$/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$/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$/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$/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>
</component>
</project>

View File

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

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,30 +0,0 @@
package minitennis1;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class Game2 extends JPanel {
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fillOval(0, 0, 30, 30);
g2d.drawOval(0, 50, 30, 30);
g2d.fillRect(50, 0, 30, 30);
g2d.drawRect(50, 50, 30, 30);
g2d.draw(new Ellipse2D.Double(0, 100, 30, 30));
}
public static void main(String[] args) {
JFrame frame = new JFrame("Mini Tennis");
frame.add(new Game2());
frame.setSize(300, 300);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

View File

@@ -1,4 +0,0 @@
package game.minitennis2;
public class Game {
}

View File

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

View File

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

View File

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

View File

@@ -1,4 +1,4 @@
package painting;
package painting.SwingPaintDemo;
import javax.swing.SwingUtilities;
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,109 @@
package sprites.shootingMissles;
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 java.util.List;
import javax.swing.JPanel;
import javax.swing.Timer;
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,27 @@
package sprites.shootingMissles;
public class Missile extends Sprite {
private final int BOARD_WIDTH = 390;
private final int MISSILE_SPEED = 2;
public Missile(int x, int y) {
super(x, y);
initMissile();
}
private void initMissile() {
loadImage("/home/bitecoding/Pictures/laser.png",35,35);
getImageDimensions();
}
public void move() {
y -= MISSILE_SPEED;
if (y < 0) {
visible = false;
}
}
}

View File

@@ -0,0 +1,85 @@
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("/home/bitecoding/Pictures/spaceship.png",50,50);
getImageDimensions();
}
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, y + height / 2));
}
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,52 @@
package sprites.shootingMissles;
import java.awt.*;
import javax.swing.ImageIcon;
public class Sprite {
protected int x;
protected int y;
protected int width;
protected int height;
protected boolean visible;
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) {
ImageIcon ii = new ImageIcon(imageName);
image = ii.getImage().getScaledInstance(width, height, Image.SCALE_DEFAULT);
}
protected void getImageDimensions() {
width = image.getWidth(null);
height = image.getHeight(null);
}
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

@@ -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>

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,4 +0,0 @@
package PACKAGE_NAME;
public class Player {
}

View File

@@ -1,134 +0,0 @@
import javax.swing.*;
import java.awt.*;
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;
}
}
}

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>

View File

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

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.