Components and supplies
1
LCD Keypad Shield For Arduino
1
Arduino Uno Rev3
Apps and platforms
1
Arduino IDE
Project description
Code
SMD_1
cpp
Smart Monopoly Device
1#include <LiquidCrystal.h> 2 3// Initialize the LCD with the pin numbers 4LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 5 6// Define button pins 7const int btnRIGHT = 0; 8const int btnUP = 1; 9const int btnDOWN = 2; 10const int btnLEFT = 3; // Using LEFT button for buying 11const int btnSELECT = 4; 12const int btnNONE = 5; 13 14// Number of players and current player 15int numPlayers = 2; 16int currentPlayer = 1; 17 18// Player positions 19int playerPositions[4] = {0, 0, 0, 0}; // Assuming a maximum of 4 players 20 21// Names of the places on the Monopoly board 22const char* places[] = { 23 "GO", "Old Kent Road", "Community Chest", "Whitechapel Road", "Income Tax", "Kings Cross Station", 24 "The Angel Islington", "Chance", "Euston Road", "Pentonville Road", "Just Visiting Jail", 25 "Pall Mall", "Electric Company", "Whitehall", "Northumberland Ave", "Marylebone Station", 26 "Bow Street", "Community Chest", "Marlborough Street", "Vine Street", "Free Parking", 27 "Strand", "Chance", "Fleet Street", "Trafalgar Square", "Fenchurch St Station", "Leicester Square", 28 "Coventry Street", "Water Works", "Piccadilly", "Go to Jail", "Regent Street", "Oxford Street", 29 "Community Chest", "Bond Street", "Liverpool St Station", "Chance", "Park Lane", "Super Tax", 30 "Mayfair" 31}; 32 33// Track ownership of the places 34int ownership[40]; // -1 means no owner, 0-3 corresponds to player indices 35 36// Indices of properties that can be bought 37const int buyableProperties[] = { 38 1, 3, 5, 6, 8, 9, 11, 13, 14, 15, 16, 18, 19, 21, 23, 24, 25, 26, 27, 29, 31, 32, 34, 37, 39 39}; 40 41void setup() { 42 lcd.begin(16, 2); // Set up the LCD's number of columns and rows 43 lcd.print("Monopoly Game"); 44 delay(2000); 45 selectNumPlayers(); 46 // Initialize ownership array to -1 (no owner) 47 for (int i = 0; i < 40; i++) { 48 ownership[i] = -1; 49 } 50} 51 52void loop() { 53 playTurn(); 54} 55 56void selectNumPlayers() { 57 lcd.clear(); 58 lcd.print("Players: "); 59 lcd.print(numPlayers); 60 delay(500); 61 62 while (true) { 63 int btn = read_LCD_buttons(); 64 if (btn == btnUP) { 65 if (numPlayers < 4) numPlayers++; 66 } else if (btn == btnDOWN) { 67 if (numPlayers > 2) numPlayers--; 68 } else if (btn == btnSELECT) { 69 break; 70 } 71 lcd.setCursor(9, 0); 72 lcd.print(numPlayers); 73 delay(200); 74 } 75} 76 77void playTurn() { 78 lcd.clear(); 79 lcd.print("Player "); 80 lcd.print(currentPlayer); 81 lcd.setCursor(0, 1); 82 lcd.print("Click SELECT"); 83 84 // Wait for SELECT button press to roll dice 85 while (true) { 86 int btn = read_LCD_buttons(); 87 if (btn == btnSELECT) { 88 break; 89 } 90 } 91 92 int diceRoll = random(1, 13); // Random number between 1 and 12 93 lcd.clear(); 94 lcd.print("Player "); 95 lcd.print(currentPlayer); 96 lcd.setCursor(0, 1); 97 lcd.print("Roll: "); 98 lcd.print(diceRoll); 99 100 delay(3000); // Display the rolled number for 3 seconds 101 102 // Update player position 103 playerPositions[currentPlayer - 1] = (playerPositions[currentPlayer - 1] + diceRoll) % 40; 104 105 // Display the place name 106 int position = playerPositions[currentPlayer - 1]; 107 lcd.clear(); 108 lcd.print(places[position]); 109 110 // Check if the place is buyable 111 bool isBuyable = false; 112 for (int i = 0; i < sizeof(buyableProperties) / sizeof(buyableProperties[0]); i++) { 113 if (position == buyableProperties[i]) { 114 isBuyable = true; 115 break; 116 } 117 } 118 119 // Check if the place is owned by another player 120 if (ownership[position] != -1 && ownership[position] != currentPlayer - 1) { 121 lcd.setCursor(0, 1); 122 lcd.print("Pay rent to P"); 123 lcd.print(ownership[position] + 1); 124 delay(3000); // Display the rent message for 3 seconds 125 } 126 127 if (isBuyable && ownership[position] == -1) { 128 // Wait for LEFT button press to buy the place or RIGHT to skip buying 129 while (true) { 130 int btn = read_LCD_buttons(); 131 if (btn == btnLEFT) { 132 ownership[position] = currentPlayer - 1; // Mark the place as owned by the current player 133 134 // Display "Bought by" message 135 lcd.clear(); 136 lcd.print("Bought by"); 137 lcd.setCursor(0, 1); 138 lcd.print("Player "); 139 lcd.print(currentPlayer); 140 141 delay(3000); // Display the "Bought by" message for 3 seconds 142 break; 143 } else if (btn == btnRIGHT) { 144 break; 145 } 146 } 147 } else { 148 delay(3000); // Display the place name for 3 seconds if not buyable 149 } 150 151 // Switch to next player 152 currentPlayer++; 153 if (currentPlayer > numPlayers) { 154 currentPlayer = 1; 155 } 156 157 lcd.clear(); 158 lcd.print("Next Player "); 159 lcd.print(currentPlayer); 160 lcd.setCursor(0, 1); 161 lcd.print("Click SELECT"); 162 163 // Wait for the next player to press SELECT 164 while (true) { 165 int btn = read_LCD_buttons(); 166 if (btn == btnSELECT) { 167 break; 168 } 169 } 170} 171 172int read_LCD_buttons() { 173 int adc_key_in = analogRead(0); // Read the value from the keypad shield 174 175 if (adc_key_in > 1000) return btnNONE; 176 if (adc_key_in < 50) return btnRIGHT; 177 if (adc_key_in < 195) return btnUP; 178 if (adc_key_in < 380) return btnDOWN; 179 if (adc_key_in < 555) return btnLEFT; 180 if (adc_key_in < 790) return btnSELECT; 181 182 return btnNONE; 183}
Comments
Only logged in users can leave comments