SlideShare a Scribd company logo
What's wrong with my code?
I am coding a battleship game that uses a dpad using C on an arduino mega which is connected
to a screen. I noticed that something is missing from my code but I can't figure out what it is and
why battleship is not running. For context, we have two buttons: one for select and one for
deselect. I need that incorporated as well.
#include <TFT_eSPI.h>
// Define the TFT display object
TFT_eSPI tft = TFT_eSPI();
// Define the dpad pins
#define UP_PIN 2
#define DOWN_PIN 3
#define LEFT_PIN 4
#define RIGHT_PIN 5
#define CONFIRM_PIN 6
#define SELECT_PIN 7
#define DESELECT_PIN 8
// Define the grid size
const int GRID_SIZE = 10;
// Define the ship lengths
const int SHIP_LENGTHS[] = {1, 2, 3, 4};
// Define the player grids
int player1Grid[GRID_SIZE][GRID_SIZE];
int player2Grid[GRID_SIZE][GRID_SIZE];
// Define the current player and ship length
int currentPlayer = 1;
int currentShipLength = 1;
// Define the ship placement position
int shipRow = 0;
int shipCol = 0;
int shipDirection = 0; // 0 = horizontal, 1 = vertical
// Function to initialize the game
void initializeGame() {
// Clear the player grids
memset(player1Grid, 0, sizeof(player1Grid));
memset(player2Grid, 0, sizeof(player2Grid));
// Set the current player and ship length
currentPlayer = 1;
currentShipLength = 1;
// Set the initial ship placement position
shipRow = 0;
shipCol = 0;
shipDirection = 0;
// Draw the player grids
tft.fillScreen(TFT_BLACK);
tft.drawRect(0, 0, tft.width() / 2 - 1, tft.height() - 1, TFT_WHITE);
tft.drawRect(tft.width() / 2, 0, tft.width() / 2 - 1, tft.height() - 1, TFT_WHITE);
// Draw the current ship
tft.drawRect(tft.width() / 2 + shipCol * 20, shipRow * 20, currentShipLength * 20 - 1, 19,
TFT_WHITE);
}
// Function to update the ship placement position
void updateShipPosition(int direction) {
// Calculate the new ship position
int newRow = shipRow;
int newCol = shipCol;
if (direction == 0) { // up
newRow--;
} else if (direction == 1) { // down
newRow++;
} else if (direction == 2) { // left
newCol--;
} else if (direction == 3) { // right
newCol++;
}
// Check if the new position is valid
if (newRow >= 0 && newRow < GRID_SIZE && newCol >= 0 && newCol < GRID_SIZE -
currentShipLength + 1) {
// Erase the current ship
tft.drawRect(tft.width() / 2 + shipCol * 20, shipRow * 20, currentShipLength * 20 - 1, 19,
TFT_BLACK);
// Update the ship position
shipRow = newRow;
shipCol = newCol;
// Draw the current ship
tft.drawRect(tft.width() / 2 + shipCol * 20, shipRow * 20, currentShipLength * 20 - 1, 19,
TFT_WHITE);
}
}
// Function to update the ship placement direction
void updateShipDirection() {
// check if the "right" button is pressed
if (digitalRead(RIGHT_BUTTON_PIN) == LOW) {
// increment the ship direction
shipDirection++;
// wrap the direction around if it goes past 3
if (shipDirection > 3) {
shipDirection = 0;
}
// draw the ship in the new direction
drawShip(shipX, shipY, shipLength, shipDirection);
}
// check if the "left" button is pressed
else if (digitalRead(LEFT_BUTTON_PIN) == LOW) {
// decrement the ship direction
shipDirection--;
// wrap the direction around if it goes below 0
if (shipDirection < 0) {
shipDirection = 3;
}
// draw the ship in the new direction
drawShip(shipX, shipY, shipLength, shipDirection);
}
}

More Related Content

Similar to What's wrong with my code- I am coding a battleship game that uses a d (1).pdf

i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
poblettesedanoree498
ย 
main.pdf java programming practice for programs
main.pdf java programming practice for programsmain.pdf java programming practice for programs
main.pdf java programming practice for programs
RavinderKSingla
ย 
Dam gate open close lpc prog
Dam gate open close lpc progDam gate open close lpc prog
Dam gate open close lpc prog
nikhil dixit
ย 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
fonecomp
ย 
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdfHow do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
footstatus
ย 
Cvim half precision floating point
Cvim half precision floating pointCvim half precision floating point
Cvim half precision floating point
tomoaki0705
ย 
Runtime Code Generation and Data Management for Heterogeneous Computing in Java
Runtime Code Generation and Data Management for Heterogeneous Computing in JavaRuntime Code Generation and Data Management for Heterogeneous Computing in Java
Runtime Code Generation and Data Management for Heterogeneous Computing in Java
Juan Fumero
ย 
Creating an Uber Clone - Part III.pdf
Creating an Uber Clone - Part III.pdfCreating an Uber Clone - Part III.pdf
Creating an Uber Clone - Part III.pdf
ShaiAlmog1
ย 
write the TODO part of the program.docx
write the TODO part of the program.docxwrite the TODO part of the program.docx
write the TODO part of the program.docx
annetnash8266
ย 
Combine the keypad and LCD codes in compliance to the following requ.pdf
Combine the keypad and LCD codes in compliance to the following requ.pdfCombine the keypad and LCD codes in compliance to the following requ.pdf
Combine the keypad and LCD codes in compliance to the following requ.pdf
forwardcom41
ย 
Embedded Systems Project 3rd Year
Embedded Systems Project 3rd YearEmbedded Systems Project 3rd Year
Embedded Systems Project 3rd Year
Andrew Kozik
ย 
Box2D with SIMD in JavaScript
Box2D with SIMD in JavaScriptBox2D with SIMD in JavaScript
Box2D with SIMD in JavaScript
Intelยฎ Software
ย 
2011.02.18 marco parenzan - modelli di programmazione per le gpu
2011.02.18   marco parenzan - modelli di programmazione per le gpu2011.02.18   marco parenzan - modelli di programmazione per le gpu
2011.02.18 marco parenzan - modelli di programmazione per le gpu
Marco Parenzan
ย 
Can you finish and write the int main for the code according to the in.pdf
Can you finish and write the int main for the code according to the in.pdfCan you finish and write the int main for the code according to the in.pdf
Can you finish and write the int main for the code according to the in.pdf
aksachdevahosymills
ย 
Xiicsmonth
XiicsmonthXiicsmonth
Xiicsmonth
rishikasahu1908
ย 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
Moriyoshi Koizumi
ย 
Code
CodeCode
Code
Bilal Atie
ย 
Functions for Nano 5 Card
Functions for Nano 5 CardFunctions for Nano 5 Card
Functions for Nano 5 Card
Omar Sanchez
ย 
The Ring programming language version 1.5 book - Part 2 of 31
The Ring programming language version 1.5 book - Part 2 of 31The Ring programming language version 1.5 book - Part 2 of 31
The Ring programming language version 1.5 book - Part 2 of 31
Mahmoud Samir Fayed
ย 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
Deepak Singh
ย 

Similar to What's wrong with my code- I am coding a battleship game that uses a d (1).pdf (20)

i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
ย 
main.pdf java programming practice for programs
main.pdf java programming practice for programsmain.pdf java programming practice for programs
main.pdf java programming practice for programs
ย 
Dam gate open close lpc prog
Dam gate open close lpc progDam gate open close lpc prog
Dam gate open close lpc prog
ย 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
ย 
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdfHow do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
How do I draw the Labview code for pneumatic cylinder(air pistion). .pdf
ย 
Cvim half precision floating point
Cvim half precision floating pointCvim half precision floating point
Cvim half precision floating point
ย 
Runtime Code Generation and Data Management for Heterogeneous Computing in Java
Runtime Code Generation and Data Management for Heterogeneous Computing in JavaRuntime Code Generation and Data Management for Heterogeneous Computing in Java
Runtime Code Generation and Data Management for Heterogeneous Computing in Java
ย 
Creating an Uber Clone - Part III.pdf
Creating an Uber Clone - Part III.pdfCreating an Uber Clone - Part III.pdf
Creating an Uber Clone - Part III.pdf
ย 
write the TODO part of the program.docx
write the TODO part of the program.docxwrite the TODO part of the program.docx
write the TODO part of the program.docx
ย 
Combine the keypad and LCD codes in compliance to the following requ.pdf
Combine the keypad and LCD codes in compliance to the following requ.pdfCombine the keypad and LCD codes in compliance to the following requ.pdf
Combine the keypad and LCD codes in compliance to the following requ.pdf
ย 
Embedded Systems Project 3rd Year
Embedded Systems Project 3rd YearEmbedded Systems Project 3rd Year
Embedded Systems Project 3rd Year
ย 
Box2D with SIMD in JavaScript
Box2D with SIMD in JavaScriptBox2D with SIMD in JavaScript
Box2D with SIMD in JavaScript
ย 
2011.02.18 marco parenzan - modelli di programmazione per le gpu
2011.02.18   marco parenzan - modelli di programmazione per le gpu2011.02.18   marco parenzan - modelli di programmazione per le gpu
2011.02.18 marco parenzan - modelli di programmazione per le gpu
ย 
Can you finish and write the int main for the code according to the in.pdf
Can you finish and write the int main for the code according to the in.pdfCan you finish and write the int main for the code according to the in.pdf
Can you finish and write the int main for the code according to the in.pdf
ย 
Xiicsmonth
XiicsmonthXiicsmonth
Xiicsmonth
ย 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
ย 
Code
CodeCode
Code
ย 
Functions for Nano 5 Card
Functions for Nano 5 CardFunctions for Nano 5 Card
Functions for Nano 5 Card
ย 
The Ring programming language version 1.5 book - Part 2 of 31
The Ring programming language version 1.5 book - Part 2 of 31The Ring programming language version 1.5 book - Part 2 of 31
The Ring programming language version 1.5 book - Part 2 of 31
ย 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
ย 

More from atexgarments

When I input the number 3- my output is -Ready! 0 Go!----- what gives-.pdf
When I input the number 3- my output is -Ready! 0 Go!----- what gives-.pdfWhen I input the number 3- my output is -Ready! 0 Go!----- what gives-.pdf
When I input the number 3- my output is -Ready! 0 Go!----- what gives-.pdf
atexgarments
ย 
When is it appropriate to use a one-sample Z-test instead of a one-sam.pdf
When is it appropriate to use a one-sample Z-test instead of a one-sam.pdfWhen is it appropriate to use a one-sample Z-test instead of a one-sam.pdf
When is it appropriate to use a one-sample Z-test instead of a one-sam.pdf
atexgarments
ย 
When planning for the scope of a project- the following are created- P.pdf
When planning for the scope of a project- the following are created- P.pdfWhen planning for the scope of a project- the following are created- P.pdf
When planning for the scope of a project- the following are created- P.pdf
atexgarments
ย 
When do think it would be advantageous to have background images- Word.pdf
When do think it would be advantageous to have background images- Word.pdfWhen do think it would be advantageous to have background images- Word.pdf
When do think it would be advantageous to have background images- Word.pdf
atexgarments
ย 
When Griffin cultured the blood from the mice injected with R cells an.pdf
When Griffin cultured the blood from the mice injected with R cells an.pdfWhen Griffin cultured the blood from the mice injected with R cells an.pdf
When Griffin cultured the blood from the mice injected with R cells an.pdf
atexgarments
ย 
What's the best explanation of a level of significance- Select one- a-.pdf
What's the best explanation of a level of significance- Select one- a-.pdfWhat's the best explanation of a level of significance- Select one- a-.pdf
What's the best explanation of a level of significance- Select one- a-.pdf
atexgarments
ย 
When configuring an 802-1X-EAP solution- what must be configured on th.pdf
When configuring an 802-1X-EAP solution- what must be configured on th.pdfWhen configuring an 802-1X-EAP solution- what must be configured on th.pdf
When configuring an 802-1X-EAP solution- what must be configured on th.pdf
atexgarments
ย 
What would you predict if Morrison and Braciale tested their influenza.pdf
What would you predict if Morrison and Braciale tested their influenza.pdfWhat would you predict if Morrison and Braciale tested their influenza.pdf
What would you predict if Morrison and Braciale tested their influenza.pdf
atexgarments
ย 
What type of unconformity does C represent- nonconformity disconformit.pdf
What type of unconformity does C represent- nonconformity disconformit.pdfWhat type of unconformity does C represent- nonconformity disconformit.pdf
What type of unconformity does C represent- nonconformity disconformit.pdf
atexgarments
ย 
What would happen to the composition of the interstitial fluid if astr.pdf
What would happen to the composition of the interstitial fluid if astr.pdfWhat would happen to the composition of the interstitial fluid if astr.pdf
What would happen to the composition of the interstitial fluid if astr.pdf
atexgarments
ย 
What would happen if NAD+ was not generated for the citric acid cycle-.pdf
What would happen if NAD+ was not generated for the citric acid cycle-.pdfWhat would happen if NAD+ was not generated for the citric acid cycle-.pdf
What would happen if NAD+ was not generated for the citric acid cycle-.pdf
atexgarments
ย 
When constructing a pro forma Balance Sheet- is-are considered as Curr (1).pdf
When constructing a pro forma Balance Sheet- is-are considered as Curr (1).pdfWhen constructing a pro forma Balance Sheet- is-are considered as Curr (1).pdf
When constructing a pro forma Balance Sheet- is-are considered as Curr (1).pdf
atexgarments
ย 
What type of programming error is an attacker likely to attempt to exp.pdf
What type of programming error is an attacker likely to attempt to exp.pdfWhat type of programming error is an attacker likely to attempt to exp.pdf
What type of programming error is an attacker likely to attempt to exp.pdf
atexgarments
ย 
what was henry wirtz executed for- what was henry wirtz executed for-.pdf
what was henry wirtz executed for- what was henry wirtz executed for-.pdfwhat was henry wirtz executed for- what was henry wirtz executed for-.pdf
what was henry wirtz executed for- what was henry wirtz executed for-.pdf
atexgarments
ย 
what would be the standard deciation of Bank B- Ba Bi Cick the ioon to.pdf
what would be the standard deciation of Bank B- Ba Bi Cick the ioon to.pdfwhat would be the standard deciation of Bank B- Ba Bi Cick the ioon to.pdf
what would be the standard deciation of Bank B- Ba Bi Cick the ioon to.pdf
atexgarments
ย 
When cells release signal molecules (ligands) to send a message- and t (1).pdf
When cells release signal molecules (ligands) to send a message- and t (1).pdfWhen cells release signal molecules (ligands) to send a message- and t (1).pdf
When cells release signal molecules (ligands) to send a message- and t (1).pdf
atexgarments
ย 
What type of volcano is shown in the ahoto- Glick to view laresimase d.pdf
What type of volcano is shown in the ahoto- Glick to view laresimase d.pdfWhat type of volcano is shown in the ahoto- Glick to view laresimase d.pdf
What type of volcano is shown in the ahoto- Glick to view laresimase d.pdf
atexgarments
ย 
When award without discussions is not stated in the solicitation and t.pdf
When award without discussions is not stated in the solicitation and t.pdfWhen award without discussions is not stated in the solicitation and t.pdf
When award without discussions is not stated in the solicitation and t.pdf
atexgarments
ย 
Which Munsell color notation has the lowest chroma- 10YR 1-2 5YR 6-7 2.pdf
Which Munsell color notation has the lowest chroma- 10YR 1-2 5YR 6-7 2.pdfWhich Munsell color notation has the lowest chroma- 10YR 1-2 5YR 6-7 2.pdf
Which Munsell color notation has the lowest chroma- 10YR 1-2 5YR 6-7 2.pdf
atexgarments
ย 
What type of value is being created by JD-com's digital and online ini.pdf
What type of value is being created by JD-com's digital and online ini.pdfWhat type of value is being created by JD-com's digital and online ini.pdf
What type of value is being created by JD-com's digital and online ini.pdf
atexgarments
ย 

More from atexgarments (20)

When I input the number 3- my output is -Ready! 0 Go!----- what gives-.pdf
When I input the number 3- my output is -Ready! 0 Go!----- what gives-.pdfWhen I input the number 3- my output is -Ready! 0 Go!----- what gives-.pdf
When I input the number 3- my output is -Ready! 0 Go!----- what gives-.pdf
ย 
When is it appropriate to use a one-sample Z-test instead of a one-sam.pdf
When is it appropriate to use a one-sample Z-test instead of a one-sam.pdfWhen is it appropriate to use a one-sample Z-test instead of a one-sam.pdf
When is it appropriate to use a one-sample Z-test instead of a one-sam.pdf
ย 
When planning for the scope of a project- the following are created- P.pdf
When planning for the scope of a project- the following are created- P.pdfWhen planning for the scope of a project- the following are created- P.pdf
When planning for the scope of a project- the following are created- P.pdf
ย 
When do think it would be advantageous to have background images- Word.pdf
When do think it would be advantageous to have background images- Word.pdfWhen do think it would be advantageous to have background images- Word.pdf
When do think it would be advantageous to have background images- Word.pdf
ย 
When Griffin cultured the blood from the mice injected with R cells an.pdf
When Griffin cultured the blood from the mice injected with R cells an.pdfWhen Griffin cultured the blood from the mice injected with R cells an.pdf
When Griffin cultured the blood from the mice injected with R cells an.pdf
ย 
What's the best explanation of a level of significance- Select one- a-.pdf
What's the best explanation of a level of significance- Select one- a-.pdfWhat's the best explanation of a level of significance- Select one- a-.pdf
What's the best explanation of a level of significance- Select one- a-.pdf
ย 
When configuring an 802-1X-EAP solution- what must be configured on th.pdf
When configuring an 802-1X-EAP solution- what must be configured on th.pdfWhen configuring an 802-1X-EAP solution- what must be configured on th.pdf
When configuring an 802-1X-EAP solution- what must be configured on th.pdf
ย 
What would you predict if Morrison and Braciale tested their influenza.pdf
What would you predict if Morrison and Braciale tested their influenza.pdfWhat would you predict if Morrison and Braciale tested their influenza.pdf
What would you predict if Morrison and Braciale tested their influenza.pdf
ย 
What type of unconformity does C represent- nonconformity disconformit.pdf
What type of unconformity does C represent- nonconformity disconformit.pdfWhat type of unconformity does C represent- nonconformity disconformit.pdf
What type of unconformity does C represent- nonconformity disconformit.pdf
ย 
What would happen to the composition of the interstitial fluid if astr.pdf
What would happen to the composition of the interstitial fluid if astr.pdfWhat would happen to the composition of the interstitial fluid if astr.pdf
What would happen to the composition of the interstitial fluid if astr.pdf
ย 
What would happen if NAD+ was not generated for the citric acid cycle-.pdf
What would happen if NAD+ was not generated for the citric acid cycle-.pdfWhat would happen if NAD+ was not generated for the citric acid cycle-.pdf
What would happen if NAD+ was not generated for the citric acid cycle-.pdf
ย 
When constructing a pro forma Balance Sheet- is-are considered as Curr (1).pdf
When constructing a pro forma Balance Sheet- is-are considered as Curr (1).pdfWhen constructing a pro forma Balance Sheet- is-are considered as Curr (1).pdf
When constructing a pro forma Balance Sheet- is-are considered as Curr (1).pdf
ย 
What type of programming error is an attacker likely to attempt to exp.pdf
What type of programming error is an attacker likely to attempt to exp.pdfWhat type of programming error is an attacker likely to attempt to exp.pdf
What type of programming error is an attacker likely to attempt to exp.pdf
ย 
what was henry wirtz executed for- what was henry wirtz executed for-.pdf
what was henry wirtz executed for- what was henry wirtz executed for-.pdfwhat was henry wirtz executed for- what was henry wirtz executed for-.pdf
what was henry wirtz executed for- what was henry wirtz executed for-.pdf
ย 
what would be the standard deciation of Bank B- Ba Bi Cick the ioon to.pdf
what would be the standard deciation of Bank B- Ba Bi Cick the ioon to.pdfwhat would be the standard deciation of Bank B- Ba Bi Cick the ioon to.pdf
what would be the standard deciation of Bank B- Ba Bi Cick the ioon to.pdf
ย 
When cells release signal molecules (ligands) to send a message- and t (1).pdf
When cells release signal molecules (ligands) to send a message- and t (1).pdfWhen cells release signal molecules (ligands) to send a message- and t (1).pdf
When cells release signal molecules (ligands) to send a message- and t (1).pdf
ย 
What type of volcano is shown in the ahoto- Glick to view laresimase d.pdf
What type of volcano is shown in the ahoto- Glick to view laresimase d.pdfWhat type of volcano is shown in the ahoto- Glick to view laresimase d.pdf
What type of volcano is shown in the ahoto- Glick to view laresimase d.pdf
ย 
When award without discussions is not stated in the solicitation and t.pdf
When award without discussions is not stated in the solicitation and t.pdfWhen award without discussions is not stated in the solicitation and t.pdf
When award without discussions is not stated in the solicitation and t.pdf
ย 
Which Munsell color notation has the lowest chroma- 10YR 1-2 5YR 6-7 2.pdf
Which Munsell color notation has the lowest chroma- 10YR 1-2 5YR 6-7 2.pdfWhich Munsell color notation has the lowest chroma- 10YR 1-2 5YR 6-7 2.pdf
Which Munsell color notation has the lowest chroma- 10YR 1-2 5YR 6-7 2.pdf
ย 
What type of value is being created by JD-com's digital and online ini.pdf
What type of value is being created by JD-com's digital and online ini.pdfWhat type of value is being created by JD-com's digital and online ini.pdf
What type of value is being created by JD-com's digital and online ini.pdf
ย 

Recently uploaded

How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
ย 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
ย 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
National Information Standards Organization (NISO)
ย 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
ย 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
ย 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
ย 
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...
Nguyen Thanh Tu Collection
ย 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
ย 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
ย 
A Independรชncia da Amรฉrica Espanhola LAPBOOK.pdf
A Independรชncia da Amรฉrica Espanhola LAPBOOK.pdfA Independรชncia da Amรฉrica Espanhola LAPBOOK.pdf
A Independรชncia da Amรฉrica Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixรฃo
ย 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
ย 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
ย 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
ย 
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDFLifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Vivekanand Anglo Vedic Academy
ย 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
nitinpv4ai
ย 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
ย 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
ย 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
ย 
Prรฉsentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Prรฉsentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrรฉsentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Prรฉsentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
ย 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
BoudhayanBhattachari
ย 

Recently uploaded (20)

How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
ย 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ย 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
ย 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
ย 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
ย 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
ย 
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...
ย 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
ย 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
ย 
A Independรชncia da Amรฉrica Espanhola LAPBOOK.pdf
A Independรชncia da Amรฉrica Espanhola LAPBOOK.pdfA Independรชncia da Amรฉrica Espanhola LAPBOOK.pdf
A Independรชncia da Amรฉrica Espanhola LAPBOOK.pdf
ย 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
ย 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
ย 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
ย 
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDFLifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
Lifelines of National Economy chapter for Class 10 STUDY MATERIAL PDF
ย 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
ย 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
ย 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
ย 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
ย 
Prรฉsentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Prรฉsentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrรฉsentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Prรฉsentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
ย 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
ย 

What's wrong with my code- I am coding a battleship game that uses a d (1).pdf

  • 1. What's wrong with my code? I am coding a battleship game that uses a dpad using C on an arduino mega which is connected to a screen. I noticed that something is missing from my code but I can't figure out what it is and why battleship is not running. For context, we have two buttons: one for select and one for deselect. I need that incorporated as well. #include <TFT_eSPI.h> // Define the TFT display object TFT_eSPI tft = TFT_eSPI(); // Define the dpad pins #define UP_PIN 2 #define DOWN_PIN 3 #define LEFT_PIN 4 #define RIGHT_PIN 5 #define CONFIRM_PIN 6 #define SELECT_PIN 7 #define DESELECT_PIN 8 // Define the grid size const int GRID_SIZE = 10; // Define the ship lengths const int SHIP_LENGTHS[] = {1, 2, 3, 4}; // Define the player grids int player1Grid[GRID_SIZE][GRID_SIZE]; int player2Grid[GRID_SIZE][GRID_SIZE]; // Define the current player and ship length int currentPlayer = 1;
  • 2. int currentShipLength = 1; // Define the ship placement position int shipRow = 0; int shipCol = 0; int shipDirection = 0; // 0 = horizontal, 1 = vertical // Function to initialize the game void initializeGame() { // Clear the player grids memset(player1Grid, 0, sizeof(player1Grid)); memset(player2Grid, 0, sizeof(player2Grid)); // Set the current player and ship length currentPlayer = 1; currentShipLength = 1; // Set the initial ship placement position shipRow = 0; shipCol = 0; shipDirection = 0; // Draw the player grids tft.fillScreen(TFT_BLACK); tft.drawRect(0, 0, tft.width() / 2 - 1, tft.height() - 1, TFT_WHITE); tft.drawRect(tft.width() / 2, 0, tft.width() / 2 - 1, tft.height() - 1, TFT_WHITE); // Draw the current ship tft.drawRect(tft.width() / 2 + shipCol * 20, shipRow * 20, currentShipLength * 20 - 1, 19, TFT_WHITE);
  • 3. } // Function to update the ship placement position void updateShipPosition(int direction) { // Calculate the new ship position int newRow = shipRow; int newCol = shipCol; if (direction == 0) { // up newRow--; } else if (direction == 1) { // down newRow++; } else if (direction == 2) { // left newCol--; } else if (direction == 3) { // right newCol++; } // Check if the new position is valid if (newRow >= 0 && newRow < GRID_SIZE && newCol >= 0 && newCol < GRID_SIZE - currentShipLength + 1) { // Erase the current ship tft.drawRect(tft.width() / 2 + shipCol * 20, shipRow * 20, currentShipLength * 20 - 1, 19, TFT_BLACK); // Update the ship position shipRow = newRow; shipCol = newCol;
  • 4. // Draw the current ship tft.drawRect(tft.width() / 2 + shipCol * 20, shipRow * 20, currentShipLength * 20 - 1, 19, TFT_WHITE); } } // Function to update the ship placement direction void updateShipDirection() { // check if the "right" button is pressed if (digitalRead(RIGHT_BUTTON_PIN) == LOW) { // increment the ship direction shipDirection++; // wrap the direction around if it goes past 3 if (shipDirection > 3) { shipDirection = 0; } // draw the ship in the new direction drawShip(shipX, shipY, shipLength, shipDirection); } // check if the "left" button is pressed else if (digitalRead(LEFT_BUTTON_PIN) == LOW) { // decrement the ship direction shipDirection--; // wrap the direction around if it goes below 0 if (shipDirection < 0) {
  • 5. shipDirection = 3; } // draw the ship in the new direction drawShip(shipX, shipY, shipLength, shipDirection); } }