#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Keypad.h>
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {2,3,4,5};
byte colPins[COLS] = {6,7,A0,A1};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
struct Player {
String name;
int balance;
};
Player players[] = {
{"PLAYER 1",1500},
{"PLAYER 2",1500},
{"PLAYER 3",1500},
{"PLAYER 4",1500}
};
int currentPlayer = 0;
struct Property {
String name;
int price;
int rent;
int owner;
bool sold;
};
Property properties[] = {
{"Old Kent Road",60,2,-1,false},
{"Whitechapel Road",60,4,-1,false},
{"The Angel Islington",100,6,-1,false},
{"Euston Road",100,6,-1,false},
{"Pentonville Road",120,8,-1,false},
{"Pall Mall",140,10,-1,false},
{"Whitehall",140,10,-1,false},
{"Northumberland Avenue",160,12,-1,false},
{"Bow Street",180,14,-1,false},
{"Marlborough Street",180,14,-1,false},
{"Vine Street",200,16,-1,false},
{"Strand",220,18,-1,false},
{"Fleet Street",220,18,-1,false},
{"Trafalgar Square",240,20,-1,false},
{"Leicester Square",260,22,-1,false},
{"Coventry Street",260,22,-1,false},
{"Piccadilly",280,24,-1,false},
{"Regent Street",300,26,-1,false},
{"Oxford Street",300,26,-1,false},
{"Bond Street",320,28,-1,false},
{"Park Lane",350,35,-1,false},
{"Mayfair",400,50,-1,false},
{"Kings Cross Station",200,25,-1,false},
{"Marylebone Station",200,25,-1,false},
{"Fenchurch St Station",200,25,-1,false},
{"Liverpool St Station",200,25,-1,false},
{"Electric Company",150,15,-1,false},
{"Water Works",150,15,-1,false}
};
int propertyIndex = 0;
void drawMenu() {
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.setCursor(10,10);
tft.print(players[currentPlayer].name);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(10,40);
tft.print("Money: $");
tft.print(players[currentPlayer].balance);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(10,80);
tft.print("1 Buy");
tft.setCursor(10,105);
tft.print("2 Next Player");
tft.setCursor(10,130);
tft.print("3 Property");
tft.setCursor(10,155);
tft.print("4 Pay Rent");
tft.setCursor(10,180);
tft.print("5 Pay Tax");
}
void showProperty() {
tft.fillScreen(ILI9341_BLUE);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(10,10);
tft.print(properties[propertyIndex].name);
tft.setCursor(10,50);
tft.print("Price: $");
tft.print(properties[propertyIndex].price);
tft.setCursor(10,80);
tft.print("Rent: $");
tft.print(properties[propertyIndex].rent);
if(properties[propertyIndex].sold == false) {
tft.setTextColor(ILI9341_GREEN);
tft.setCursor(10,120);
tft.print("AVAILABLE");
} else {
tft.setTextColor(ILI9341_RED);
tft.setCursor(10,120);
tft.print("OWNER:");
tft.setCursor(10,145);
tft.print(players[properties[propertyIndex].owner].name);
}
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(10,210);
tft.print("A NEXT B MENU");
}
void buyProperty() {
if(properties[propertyIndex].sold == false) {
int price = properties[propertyIndex].price;
if(players[currentPlayer].balance >= price) {
players[currentPlayer].balance -= price;
properties[propertyIndex].sold = true;
properties[propertyIndex].owner = currentPlayer;
tft.fillScreen(ILI9341_GREEN);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.setCursor(20,100);
tft.print("BOUGHT!");
delay(2000);
} else {
tft.fillScreen(ILI9341_RED);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.setCursor(20,100);
tft.print("NO MONEY");
delay(2000);
}
} else {
tft.fillScreen(ILI9341_RED);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.setCursor(20,100);
tft.print("SOLD OUT");
delay(2000);
}
drawMenu();
}
void payRent() {
if(properties[propertyIndex].sold == true &&
properties[propertyIndex].owner != currentPlayer) {
int owner = properties[propertyIndex].owner;
int rent = properties[propertyIndex].rent;
players[currentPlayer].balance -= rent;
players[owner].balance += rent;
tft.fillScreen(ILI9341_MAGENTA);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.setCursor(20,100);
tft.print("RENT PAID");
delay(2000);
} else {
tft.fillScreen(ILI9341_RED);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.setCursor(40,100);
tft.print("NO RENT");
delay(2000);
}
drawMenu();
}
void payTax() {
players[currentPlayer].balance -= 100;
tft.fillScreen(ILI9341_ORANGE);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.setCursor(40,100);
tft.print("TAX PAID");
delay(2000);
drawMenu();
}
void setup() {
tft.begin();
tft.setRotation(1);
drawMenu();
}
void loop() {
char key = keypad.getKey();
if(key) {
if(key == '1') {
buyProperty();
}
else if(key == '2') {
currentPlayer++;
if(currentPlayer > 3) {
currentPlayer = 0;
}
drawMenu();
}
else if(key == '3') {
showProperty();
}
else if(key == '4') {
payRent();
}
else if(key == '5') {
payTax();
}
else if(key == 'A') {
propertyIndex++;
if(propertyIndex > 27) {
propertyIndex = 0;
}
showProperty();
}
else if(key == 'B') {
drawMenu();
}
}
}