From 88a5ded3f683a35c0d8ecc012035076c0496b834 Mon Sep 17 00:00:00 2001 From: Michael Kowalczyk Date: Fri, 8 Nov 2024 09:57:57 -0500 Subject: [PATCH] Day 43 --- .../CoinInventory.java | 0 .../Item.java | 0 .../Player.java | 0 .../Item.java | 0 .../Player.java | 68 +++++++++++++++++++ .../CoinInventory.java | 1 + .../Improve the design - refactored/Item.java | 19 ++++++ .../Player.java | 10 +-- .../notes.txt | 0 .../chess program design.txt | 27 ++++++++ .../requirements and spec.txt | 27 ++++++++ 11 files changed, 145 insertions(+), 7 deletions(-) rename {Day 40 (object oriented design) => Day 42 (object oriented design)}/Improve the design - not refactored/CoinInventory.java (100%) rename {Day 40 (object oriented design) => Day 42 (object oriented design)}/Improve the design - not refactored/Item.java (100%) rename {Day 40 (object oriented design) => Day 42 (object oriented design)}/Improve the design - not refactored/Player.java (100%) rename {Day 40 (object oriented design)/Improve the design - refactored => Day 42 (object oriented design)/Improve the design - refactored more}/Item.java (100%) create mode 100644 Day 42 (object oriented design)/Improve the design - refactored more/Player.java rename {Day 40 (object oriented design) => Day 42 (object oriented design)}/Improve the design - refactored/CoinInventory.java (82%) create mode 100644 Day 42 (object oriented design)/Improve the design - refactored/Item.java rename {Day 40 (object oriented design) => Day 42 (object oriented design)}/Improve the design - refactored/Player.java (82%) rename {Day 40 (object oriented design) => Day 42 (object oriented design)}/notes.txt (100%) create mode 100644 Day 43 (design chess application)/chess program design.txt create mode 100644 Day 43 (design chess application)/requirements and spec.txt diff --git a/Day 40 (object oriented design)/Improve the design - not refactored/CoinInventory.java b/Day 42 (object oriented design)/Improve the design - not refactored/CoinInventory.java similarity index 100% rename from Day 40 (object oriented design)/Improve the design - not refactored/CoinInventory.java rename to Day 42 (object oriented design)/Improve the design - not refactored/CoinInventory.java diff --git a/Day 40 (object oriented design)/Improve the design - not refactored/Item.java b/Day 42 (object oriented design)/Improve the design - not refactored/Item.java similarity index 100% rename from Day 40 (object oriented design)/Improve the design - not refactored/Item.java rename to Day 42 (object oriented design)/Improve the design - not refactored/Item.java diff --git a/Day 40 (object oriented design)/Improve the design - not refactored/Player.java b/Day 42 (object oriented design)/Improve the design - not refactored/Player.java similarity index 100% rename from Day 40 (object oriented design)/Improve the design - not refactored/Player.java rename to Day 42 (object oriented design)/Improve the design - not refactored/Player.java diff --git a/Day 40 (object oriented design)/Improve the design - refactored/Item.java b/Day 42 (object oriented design)/Improve the design - refactored more/Item.java similarity index 100% rename from Day 40 (object oriented design)/Improve the design - refactored/Item.java rename to Day 42 (object oriented design)/Improve the design - refactored more/Item.java diff --git a/Day 42 (object oriented design)/Improve the design - refactored more/Player.java b/Day 42 (object oriented design)/Improve the design - refactored more/Player.java new file mode 100644 index 0000000..7c13089 --- /dev/null +++ b/Day 42 (object oriented design)/Improve the design - refactored more/Player.java @@ -0,0 +1,68 @@ +// Here we merged the CoinInventory in with the Player class. +// The class still represents one coherent concept (a player), has only 3 instance variables, and is easy to understand. +// This is ok design unless there is a need for reusing CoinInventory elsewhere in the program. + +import java.util.*; + +public class Player { + private String name; + private int[] coinCounts; //cp (pennies), sp (dimes), ep (half-dollars), gp (dollar bills), pp (five dollar bills) + private ArrayList otherInventory; + + public Player(String name) { + this.name = name; + otherInventory = new ArrayList(); + coinCounts = new int[5]; + } + + public void addItem(Item item) { + otherInventory.add(item); + } + + public void collectMoney(int cp, int sp, int ep, int gp, int pp) { + coinCounts[0] += cp; + coinCounts[1] += sp; + coinCounts[2] += ep; + coinCounts[3] += gp; + coinCounts[4] += pp; + } + + public double getRunningSpeed() { + double speed = 5 * (1 - getEncumbrance() / 1000.0); //Speed 5 if unencumbered; speed 0 if 1000 units carried or more. + if (speed < 0) { + return 0; + } + else { + return speed; + } + } + + public double getMoneyValue() { + return .01 * coinCounts[0] + .1 * coinCounts[1] + .5 * coinCounts[2] + 1 * coinCounts[3] + 5 * coinCounts[4]; + } + + public int getEncumbrance() { + int encumbrance = coinCounts[0] + coinCounts[1] + coinCounts[2] + coinCounts[3] + coinCounts[4]; + for(Item i : otherInventory) encumbrance += i.getWeight(); + return encumbrance; + } + + //Test method. + public static void main(String[] args) { + Player p = new Player("Whistlestick"); + Item sword = new Item("Magic sword +2", 20); + Item shield = new Item("Normal shield", 40); + p.addItem(sword); + p.addItem(shield); + if (p.getEncumbrance() != 60) throw new RuntimeException("getEncumbrance() should be 60, not " + p.getEncumbrance()); + p.collectMoney(10,2,0,40,2); + if (p.getEncumbrance() != 60+54) throw new RuntimeException("getEncumbrance() should be 114, not " + p.getEncumbrance()); + if (p.getMoneyValue() != 50.3) throw new RuntimeException("getMoneyValue() should be 50.3, not " + p.getMoneyValue()); + p.collectMoney(0,0,0,6,0); + if (p.getEncumbrance() != 120) throw new RuntimeException("getEncumbrance() should be 120, not " + p.getEncumbrance()); + if (p.getRunningSpeed() != 4.4) throw new RuntimeException("getRunningSpeed() should be 4.4, not " + p.getRunningSpeed()); + System.out.println("All tests passed."); + } + + +} \ No newline at end of file diff --git a/Day 40 (object oriented design)/Improve the design - refactored/CoinInventory.java b/Day 42 (object oriented design)/Improve the design - refactored/CoinInventory.java similarity index 82% rename from Day 40 (object oriented design)/Improve the design - refactored/CoinInventory.java rename to Day 42 (object oriented design)/Improve the design - refactored/CoinInventory.java index 614e7f6..72aa66c 100644 --- a/Day 40 (object oriented design)/Improve the design - refactored/CoinInventory.java +++ b/Day 42 (object oriented design)/Improve the design - refactored/CoinInventory.java @@ -1,3 +1,4 @@ +//We took the code that deals with coinCounts and moved it into this class. The weight variable is eliminated too. This is better design. public class CoinInventory { private int[] coinCounts; //cp (pennies), sp (dimes), ep (half-dollars), gp (dollar bills), pp (five dollar bills) diff --git a/Day 42 (object oriented design)/Improve the design - refactored/Item.java b/Day 42 (object oriented design)/Improve the design - refactored/Item.java new file mode 100644 index 0000000..c64a56f --- /dev/null +++ b/Day 42 (object oriented design)/Improve the design - refactored/Item.java @@ -0,0 +1,19 @@ +//This class remained unchanged from the original. + +public class Item { + private String description; + private int weight; + + public Item(String description, int weight) { + this.description = description; + this.weight = weight; + } + + public int getWeight() { + return weight; + } + + public String getDescription() { + return description; + } +} diff --git a/Day 40 (object oriented design)/Improve the design - refactored/Player.java b/Day 42 (object oriented design)/Improve the design - refactored/Player.java similarity index 82% rename from Day 40 (object oriented design)/Improve the design - refactored/Player.java rename to Day 42 (object oriented design)/Improve the design - refactored/Player.java index 22d97be..39c1cd4 100644 --- a/Day 40 (object oriented design)/Improve the design - refactored/Player.java +++ b/Day 42 (object oriented design)/Improve the design - refactored/Player.java @@ -1,11 +1,7 @@ -//The design among these 3 classes needs improvement, and the code has a couple of bugs, too. -//Improve the design and fix the bugs. -//The public interface of Player should remain the same as it is now. -//Hints: - //Avoid duplicate data - //Methods and instance data should have high cohesion - //Coupling should be loose between classes +// The encumbrance variable is eliminated - it was duplicate data since we can derive that from the items themselves. +// This class no longer needs to understand the internal implementation of CoinInventory (i.e. which elements of the int array represented which coin). +// The addItem(), collectMoney(), and getMoneyValue() methods are now wrappers for the CoinInventory class. That's ok in this circumstance. import java.util.*; diff --git a/Day 40 (object oriented design)/notes.txt b/Day 42 (object oriented design)/notes.txt similarity index 100% rename from Day 40 (object oriented design)/notes.txt rename to Day 42 (object oriented design)/notes.txt diff --git a/Day 43 (design chess application)/chess program design.txt b/Day 43 (design chess application)/chess program design.txt new file mode 100644 index 0000000..5b0737e --- /dev/null +++ b/Day 43 (design chess application)/chess program design.txt @@ -0,0 +1,27 @@ +Possible classes: + GUI + Display the game board + Show the selected first half of the move + Show the list of moves that are performed so far + Show the captured pieces + Show highlighting for the most recent move + GameBoard + Direct storage of piece data in a 2D array (speed of computation) + Knowledge of whether or not castling, en passant are available + Be able to set up the starting positions + Perform the move (remove piece if a capture, promote to queen if get to the end) + Piece (subclasses?) + Symbol + Position (else captured) + Player + Enacts moves on the GameBoard + +Where are the rules going to be programmed in? + A piece has to be aware of other stuff going on around it in order to calculate if a move is legal or not. + +What is a piece location useful for? + Checking for legality of moves + Drawing the pieces on the board + +Duplicating game states will be easier if we don't have lots of objects attached to the game board. + \ No newline at end of file diff --git a/Day 43 (design chess application)/requirements and spec.txt b/Day 43 (design chess application)/requirements and spec.txt new file mode 100644 index 0000000..b4cc9aa --- /dev/null +++ b/Day 43 (design chess application)/requirements and spec.txt @@ -0,0 +1,27 @@ +Chess program +============= +This simple application allows the user to play chess. Moves can be made by human or computer. + +GUI +=== +The GUI shows a 2D grid of colored chess squares and whichever pieces are in play (it starts with a new game, ready to play). + +Text feedback is given for the game state (who's turn it is, and if it is checkmate or stalemate). + +The user makes a move by clicking the source and destination square (for castling and en passant move the king or pawn to its natural destination square). + Any click (if it is one of your pieces) highlights that square + If some square highlighted, then: + if second click is legal move, do it and unhighlight + otherwise unhighlight with no move + else unhighlight without moving + + +An "AI move" button makes the computer move (choose a random move from those available). + +An "Undo" button takes back the most recent move. + +A "Reset" button puts the board in its correct configuration for the first turn of the game. + +Game rules +========== +Standard chess rules apply. The 3-move-repetition, 50-move rule, and underpromotion are not implemented in this version.