diff --git a/CCI_Java/CCI_Java/src/version2/SnakeGame.java b/CCI_Java/CCI_Java/src/version2/SnakeGame.java index a5f0b8240d9e90ca82c4d8c22102f84f55e8e472..3245aac62cac0df38526132e25a757a51bbf53bc 100644 --- a/CCI_Java/CCI_Java/src/version2/SnakeGame.java +++ b/CCI_Java/CCI_Java/src/version2/SnakeGame.java @@ -9,23 +9,31 @@ public class SnakeGame { private Direction direction; private boolean gameOver; private Position apple; + public SnakeGame() { - initialize(); + initialize(); } + public void initialize() { + } + public void advance() { } + public boolean isGameOver() { - return gameOver; + return gameOver; } + public void changeDirection(Direction direction) { } + public boolean isSnakeAt(Position position) { - return false; + return false; } + public boolean isAppleAt(Position position) { - return false; + return false; } } diff --git a/CCI_Java/CCI_Java/src/version3/Direction.java b/CCI_Java/CCI_Java/src/version3/Direction.java new file mode 100644 index 0000000000000000000000000000000000000000..667776764734dab20324610fbef18bb11a79578b --- /dev/null +++ b/CCI_Java/CCI_Java/src/version3/Direction.java @@ -0,0 +1,5 @@ +package version3; + +public enum Direction { + NORTH, SOUTH, EAST, WEST +} diff --git a/CCI_Java/CCI_Java/src/version3/MainFrame.java b/CCI_Java/CCI_Java/src/version3/MainFrame.java new file mode 100644 index 0000000000000000000000000000000000000000..00b57403dd963f6c592a720bc86f9dce4ec8c8ef --- /dev/null +++ b/CCI_Java/CCI_Java/src/version3/MainFrame.java @@ -0,0 +1,7 @@ +package version3; + +public class MainFrame { + public static void main(String[] args) { + new SnakeGameFrame().setVisible(true); + } +} diff --git a/CCI_Java/CCI_Java/src/version3/Position.java b/CCI_Java/CCI_Java/src/version3/Position.java new file mode 100644 index 0000000000000000000000000000000000000000..6642f97667645e36bd9f8ffe288c31482a9e37e2 --- /dev/null +++ b/CCI_Java/CCI_Java/src/version3/Position.java @@ -0,0 +1,47 @@ +package version3; + +public class Position { + public final int line; + public final int column; + + public Position(int line, int column) { + this.line = line; + this.column = column; + } + + public boolean isSamePosition(Position pos) { + if (isLine(pos.line) && isColumn(pos.column)) + return true; + return false; + } + + public boolean equals(Object obj) { + if (obj.getClass() == this.getClass()) + return isSamePosition((Position) obj); + return false; + + } + + public boolean isColumn(int col) { + return col == column; + } + + public boolean isLine(int line) { + return line == this.line; + } + + public Position getNext(Direction direction) { + if (direction == Direction.NORTH) + return new Position(line + 1, column); + if (direction == Direction.EAST) + return new Position(line, column + 1); + if (direction == Direction.SOUTH) + return new Position(line - 1, column); + return new Position(line, column - 1); + } + + @Override + public String toString() { + return "(" + column + "," + line + ")"; + } +} diff --git a/CCI_Java/CCI_Java/src/version3/SnakeGame.java b/CCI_Java/CCI_Java/src/version3/SnakeGame.java new file mode 100644 index 0000000000000000000000000000000000000000..68091ff4bbb7a8f78beebd500c77325f3a5bfbf4 --- /dev/null +++ b/CCI_Java/CCI_Java/src/version3/SnakeGame.java @@ -0,0 +1,82 @@ +package version3; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class SnakeGame { + public static final int GRID_LINES = 12; + public static final int GRID_COLUMNS = 12; + private List<Position> segments; + private Direction direction; + private boolean gameOver; + private Position apple; + + public SnakeGame() { + initialize(); + } + + public void initialize() { + direction = Direction.NORTH; + int startL = GRID_LINES / 2; + int startC = GRID_COLUMNS / 2; + gameOver = false; + setApple(); + segments = new ArrayList<Position>(); + segments.add(new Position(startL, startC)); + segments.add(new Position(startL + 1, startC)); + segments.add(new Position(startL + 2, startC)); + + } + + public void setApple() { + Random rand = new Random(); + apple = new Position(rand.nextInt(GRID_LINES + 1), rand.nextInt(GRID_LINES + 1)); + } + + public void advance() { + } + + public boolean isGameOver() { + return gameOver; + } + + public void changeDirection(Direction direction) { + } + + public boolean isSnakeAt(Position position) { + for (Position pos : segments) + if (position.isSamePosition(pos)) + return true; + return false; + } + + + public boolean isAppleAt(Position position) { + return position.isSamePosition(apple); + } + + public void print() { + for (int L =0; L < GRID_LINES; L++) { + for (int c =0; c<GRID_COLUMNS;c++) { + Position tmp = new Position(L, c); + if(isAppleAt(tmp)) + System.out.print(" A "); + else if(isSnakeAt(tmp)) + System.out.print(" S "); + else + System.out.print(" _ "); + } + System.out.println(); + } + } + +// public void printLine() { +// String line = ""; +// for (int c =0; c<GRID_COLUMNS;c++) { +// if(apple.isColumn(c) && apple.isLine(c)) +// } +// +// } + +} diff --git a/CCI_Java/CCI_Java/src/version3/SnakeGameFrame.java b/CCI_Java/CCI_Java/src/version3/SnakeGameFrame.java new file mode 100644 index 0000000000000000000000000000000000000000..a3ca05c213d27c2e7ec8cd877c51c4b28868ad14 --- /dev/null +++ b/CCI_Java/CCI_Java/src/version3/SnakeGameFrame.java @@ -0,0 +1,19 @@ +package version3; + + +import java.awt.Dimension; + +import javax.swing.JFrame; +import javax.swing.WindowConstants; + +@SuppressWarnings("serial") + +public class SnakeGameFrame extends JFrame { + + public SnakeGameFrame() { + setTitle("Snake"); + setSize(new Dimension(400, 400)); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + add(new SnakeGamePanel()); + } +} diff --git a/CCI_Java/CCI_Java/src/version3/SnakeGamePanel.java b/CCI_Java/CCI_Java/src/version3/SnakeGamePanel.java new file mode 100644 index 0000000000000000000000000000000000000000..809db9fa4eaf323c6cd8ef798c14692bfc8d729f --- /dev/null +++ b/CCI_Java/CCI_Java/src/version3/SnakeGamePanel.java @@ -0,0 +1,98 @@ +package version3; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.util.Timer; +import java.util.TimerTask; + +import javax.swing.JPanel; + +@SuppressWarnings("serial") +public class SnakeGamePanel extends JPanel { + private final SnakeGame snakeGame = new SnakeGame(); + private final Timer timer = new Timer(); + private static final int MIN_BORDER_SIZE = 40; + + public SnakeGamePanel() { + initializeKeyListener(); + initializeTimer(); + + } + + @Override + public void paint(Graphics g) { + for (int line = 0; line < SnakeGame.GRID_LINES; line++) + for (int column = 0; column < SnakeGame.GRID_COLUMNS; column++) + paintPosition(g, line, column); + } + + private void paintPosition(Graphics g, int line, int column) { + int x = getX(column); + int y = getY(line); + + g.setColor(getColorAt(line, column)); + g.fillRect(x, y, getCellSize(), getCellSize()); + + g.setColor(Color.LIGHT_GRAY); + g.drawRect(x, y, getCellSize(), getCellSize()); + } + + private Color getColorAt(int line, int column) { + if (snakeGame.isSnakeAt(new Position(line, column))) + return snakeGame.isGameOver() ? Color.MAGENTA : Color.GREEN; + return snakeGame.isAppleAt(new Position(line, column)) ? Color.RED : Color.BLUE; + } + + public int getCellSize() { + int cellMaximumHeight = (getHeight() - (2 * MIN_BORDER_SIZE)) / SnakeGame.GRID_LINES; + int cellMaximumWidth = (getWidth() - (2 * MIN_BORDER_SIZE)) / SnakeGame.GRID_COLUMNS; + return Math.min(cellMaximumWidth, cellMaximumHeight); + } + + private int getXBorder() { + return (getWidth() - (SnakeGame.GRID_COLUMNS * getCellSize())) / 2; + } + + private int getYBorder() { + return (getHeight() - (SnakeGame.GRID_LINES * getCellSize())) / 2; + } + + private int getX(int column) { + return getXBorder() + column * getCellSize(); + } + + private int getY(int line) { + return getYBorder() + line * getCellSize(); + } + + private void initializeKeyListener() { + setFocusable(true); + addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_UP) + snakeGame.changeDirection(Direction.SOUTH); + else if (e.getKeyCode() == KeyEvent.VK_DOWN) + snakeGame.changeDirection(Direction.NORTH); + else if (e.getKeyCode() == KeyEvent.VK_LEFT) + snakeGame.changeDirection(Direction.WEST); + else if (e.getKeyCode() == KeyEvent.VK_RIGHT) + snakeGame.changeDirection(Direction.EAST); + else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) + snakeGame.initialize(); + } + }); + } + + private void initializeTimer() { + timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + snakeGame.advance(); + repaint(); + } + }, 1000, 200); + } +} \ No newline at end of file diff --git a/CCI_Java/CCI_Java/src/version3/TestDirection.java b/CCI_Java/CCI_Java/src/version3/TestDirection.java new file mode 100644 index 0000000000000000000000000000000000000000..92bbb729c12065b86bc3fe97c1bd449109652cd4 --- /dev/null +++ b/CCI_Java/CCI_Java/src/version3/TestDirection.java @@ -0,0 +1,15 @@ +package version3; + +public class TestDirection { + + public static void main(String[] args) { + Direction direction1 = null; + System.out.println("Direction initiale : " + direction1); + direction1 = Direction.NORTH; + System.out.println("Nouvelle direction : " + direction1); + System.out.println(direction1 == Direction.NORTH); + System.out.println(direction1 == Direction.EAST); + } + + +} diff --git a/CCI_Java/CCI_Java/src/version3/TestInitialize.java b/CCI_Java/CCI_Java/src/version3/TestInitialize.java new file mode 100644 index 0000000000000000000000000000000000000000..4b2727df8b8162688b0e3019a32f4dc3ab0328d5 --- /dev/null +++ b/CCI_Java/CCI_Java/src/version3/TestInitialize.java @@ -0,0 +1,11 @@ +package version3; + +public class TestInitialize { + + public static void main(String[] args) { + SnakeGame game = new SnakeGame(); + game.print(); + + } + +} diff --git a/CCI_Java/CCI_Java/src/version3/TestPosition.java b/CCI_Java/CCI_Java/src/version3/TestPosition.java new file mode 100644 index 0000000000000000000000000000000000000000..918c17f1b5f3d56e5eb26597e774e56714eaf96e --- /dev/null +++ b/CCI_Java/CCI_Java/src/version3/TestPosition.java @@ -0,0 +1,11 @@ +package version3; + +public class TestPosition { + + public static void main(String[] args) { + + Position pos1 = new Position(3, 4); + System.out.println(pos1); + } + +}