Segmentation fault when trying to delete window - c

I only need the WINDOW for the animation at the start and main_menu but after I get thru the if statement in main_menu that would delete win, I get segmentation fault and I just can't get rid of it. I want to delete it because when I try to clear it, the border and "Sneak" disappear, but "Play, Rules, Credits and Quit" stay on screen. Any idea on how to delete it properly?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#include <time.h>
#include <unistd.h>
void main_menu(int xm, int ym);
void rules(int xm, int ym);
int main(){
initscr();
noecho();
curs_set(0);
int xm, ym;
getmaxyx(stdscr, ym, xm);
WINDOW *win = newwin(ym/2, xm/2, ym/4, xm/4);
box(win, 0, 0);
mvwprintw(win, ym/4 - 3, xm/4 - 2, "Play");
mvwprintw(win, ym/4, xm/4 - 4, "Options");
mvwprintw(win, ym/4 + 3, xm/4 - 2, "Quit");
wrefresh(win);
int anim = 2;
int intro = 3;
clock_t start_time;
while(1){
wclear(win);
box(win, 0, 0);
mvwprintw(win, 0, anim, "Snake");
mvwprintw(win, ym/4, xm/4 - 5, "Loading...");
wrefresh(win);
start_time = clock();
while(clock() < start_time + 30000){}
if(intro == 3){
if(anim < xm/2 - 7) anim++;
else intro--;
}
if(intro == 2){
if(anim > 2) anim--;
else intro--;
}
if(intro == 1){
if(anim < xm/4 - 2) anim++;
else intro--;
}
if(intro == 0) break;
}
wborder(win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
wrefresh(win);
delwin(win);
main_menu(xm, ym);
endwin();
return 0;
}
void main_menu(int xm, int ym){
WINDOW *win = newwin(ym/2, xm/2, ym/4, xm/4);
wclear(win);
box(win, 0, 0);
mvwprintw(win, 0, xm/4 - 2, "Snake");
wrefresh(win);
keypad(win, TRUE);
int selected = 1;
int slider = 0;
wattron(win, A_STANDOUT);
mvwprintw(win, ym/4 - 4, xm/4 - 2, "Play");
wattroff(win, A_STANDOUT);
mvwprintw(win, ym/4 - 1, xm/4 - 3, "Rules");
mvwprintw(win, ym/4 + 2, xm/4 - 3, "Credits");
mvwprintw(win, ym/4 + 5, xm/4 - 2, "Quit");
while(1){
slider = wgetch(win);
if(slider == KEY_UP){
if(selected != 1) selected--;
}
if(slider == KEY_DOWN){
if(selected != 4) selected++;
}
if(slider == 10){
wborder(win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
wrefresh(win);
delwin(win);
if(selected == 2) rules(xm, ym);
}
switch(selected){
case 1:
wattron(win, A_STANDOUT);
mvwprintw(win, ym/4 - 4, xm/4 - 2, "Play");
wattroff(win, A_STANDOUT);
mvwprintw(win, ym/4 - 1, xm/4 - 3, "Rules");
mvwprintw(win, ym/4 + 2, xm/4 - 3, "Credits");
mvwprintw(win, ym/4 + 5, xm/4 - 2, "Quit");
break;
case 2:
wattron(win, A_STANDOUT);
mvwprintw(win, ym/4 - 1, xm/4 - 3, "Rules");
wattroff(win, A_STANDOUT);
mvwprintw(win, ym/4 - 4, xm/4 - 2, "Play");
mvwprintw(win, ym/4 + 2, xm/4 - 3, "Credits");
mvwprintw(win, ym/4 + 5, xm/4 - 2, "Quit");
break;
case 3:
wattron(win, A_STANDOUT);
mvwprintw(win, ym/4 + 2, xm/4 - 3, "Credits");
wattroff(win, A_STANDOUT);
mvwprintw(win, ym/4 - 4, xm/4 - 2, "Play");
mvwprintw(win, ym/4 - 1, xm/4 - 3, "Rules");
mvwprintw(win, ym/4 + 5, xm/4 - 2, "Quit");
break;
case 4:
wattron(win, A_STANDOUT);
mvwprintw(win, ym/4 + 5, xm/4 - 2, "Quit");
wattroff(win, A_STANDOUT);
mvwprintw(win, ym/4 - 4, xm/4 - 2, "Play");
mvwprintw(win, ym/4 - 1, xm/4 - 3, "Rules");
mvwprintw(win, ym/4 + 2, xm/4 - 3, "Credits");
break;
default:
mvwprintw(win, ym/4 - 4, xm/4 - 2, "Play");
mvwprintw(win, ym/4 - 1, xm/4 - 3, "Rules");
mvwprintw(win, ym/4 + 2, xm/4 - 3, "Credits");
mvwprintw(win, ym/4 + 3, xm/4 - 2, "Quit");
break;
}
wrefresh(win);
}
}
void rules(int xm, int ym){
clear();
box(win, 0, 0);
// ─│╲╱
refresh();
}
I would prefer to delete the window rather than clearing it, since I will only use it for main_menu() and maybe the main game.

The window is deleted whenever the user presses the ENTER/RETURN key
if (slider == 10) {
/* this section looks copy-pasted from `main` */
wborder(win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
wrefresh(win);
delwin(win); /* <----------- */
if (selected == 2)
rules(xm, ym);
}
Even if selected is 2, rules will eventually return back to main_menu and execution will continue.
In any case, win is always used in the switch statement:
switch (selected) {
case 1:
wattron(win, A_STANDOUT);
/* ... */
This means you are operating on a dangling pointer.
For something straightforward, you may find it easier to manage each "scene" if they are clearly separate from one another.
Here's a cursory refactoring / outline:
#include <curses.h>
#include <string.h>
enum MENU_OPTIONS { MENU_PLAY, MENU_RULES, MENU_CREDITS, MENU_QUIT, MENU_LENGTH };
static const char *menu_entries[] = { "Play", "Rules", "Credits", "Quit" };
void print_list(WINDOW *w, const char **items, int length, int highlight,
int y_origin, int x_origin, int spacing)
{
for (int i = 0; i < length; i++) {
if (highlight == i)
wattron(w, A_STANDOUT);
mvwprintw(w,
y_origin - (spacing * length / 2) + (i * spacing),
x_origin - strlen(items[i]) / 2,
"%s", items[i]);
if (highlight == i)
wattroff(w, A_STANDOUT);
}
}
int main_menu(int xm, int ym, int selected)
{
WINDOW *win = newwin(ym/2, xm/2, ym/4, xm/4);
keypad(win, TRUE);
while (1) {
werase(win);
box(win, 0, 0);
mvwprintw(win, 0, xm/4 - 2, "Snake");
print_list(win, menu_entries, MENU_LENGTH,
selected, ym / 4, xm / 4, 2);
wrefresh(win);
int input = wgetch(win);
if (KEY_UP == input || 'w' == input) {
if (selected != 0) selected--;
}
if (KEY_DOWN == input || 's' == input) {
if (selected != 3) selected++;
}
if ('\n' == input || ' ' == input)
break;
}
wclear(win);
wrefresh(win);
delwin(win);
return selected;
}
void fake_loading(int xm, int ym)
{
WINDOW *win = newwin(ym/2, xm/2, ym/4, xm/4);
int anim = 2;
int intro = 3;
while (1) {
werase(win);
box(win, 0, 0);
mvwprintw(win, 0, anim, "Snake");
mvwprintw(win, ym/4, xm/4 - 5, "Loading...");
wrefresh(win);
napms(30);
if (intro == 3) {
if (anim < xm/2 - 7) anim++;
else intro--;
}
if (intro == 2) {
if(anim > 2) anim--;
else intro--;
}
if (intro == 1) {
if(anim < xm/4 - 2) anim++;
else intro--;
}
if (intro == 0) break;
}
wclear(win);
wrefresh(win);
delwin(win);
}
void rules(int xm, int ym)
{
WINDOW *win = newwin(3, 30, ym / 2 - 1, xm / 2 - 15);
int wxm, wym;
getmaxyx(win, wym, wxm);
box(win, 0, 0);
mvwprintw(win, 0, wxm / 2 - 6, "Snake - Rules");
mvwprintw(win, 1, wxm / 2 - 5, "Eat stuff.");
wrefresh(win);
wgetch(win);
wclear(win);
wrefresh(win);
delwin(win);
}
int main(void)
{
initscr();
noecho();
curs_set(0);
int xm, ym;
getmaxyx(stdscr, ym, xm);
int running = 1;
int last_sel = 0;
fake_loading(xm, ym);
while (running) {
erase();
last_sel = main_menu(xm, ym, last_sel);
switch (last_sel) {
case MENU_RULES:
rules(xm, ym);
break;
case MENU_QUIT:
running = 0;
break;
}
refresh();
}
endwin();
}

Related

Why don't appear window created by ncurses?

A new window with input must appear. But instead just a standard ncurses's window displays.
#define _POSIX_C_SOURCE 200201L
#include <stdlib.h>
#include <curses.h>
#include <time.h>
int main () {
srand(time(NULL));
initscr();
cbreak();
noecho();
// get screen sizes
int yMax, xMax;
getmaxyx(stdscr, yMax, xMax);
//create a new window for input
int height = 10;
int width = 120;
WINDOW * inputwin = newwin(height, width, yMax/2 - 5, (xMax/2 - width/2));
box(inputwin, 0, 0);
refresh();
wrefresh(inputwin);
// get amount roads
echo();
mvwprintw(inputwin, 4, width/2 - 38, "Press key left to choose beginner level, key right - intermediate, key up - advanced.");
int amount_roads = 0;
keypad(inputwin, TRUE);
int a;
while (a != KEY_DOWN) {
int c = mvwgetch(inputwin, 5, 50);
if (c == KEY_LEFT) {
mvwprintw(inputwin, 5, 50, "You chose beginner ");
amount_roads = 2;
} else if (c == KEY_RIGHT) {
mvwprintw(inputwin, 5, 50, "You chose intermediate ");
amount_roads = 3;
} else if (c == KEY_UP) {
mvwprintw(inputwin, 5, 50, "You chose advanced ");
amount_roads = 4;
} else mvwprintw(inputwin, 5, 50, "INCORRECT INPUT. TRY AGAIN");
wrefresh(inputwin);
mvwprintw(inputwin, 6, 47, "Press key down to continue");
a = mvwgetch(inputwin, 7, 60);
}
delwin(inputwin);
wrefresh(stdscr);
//create win for game
height = yMax - 4;
width = xMax - 10;
WINDOW * gamewin = newwin(height, width, 2, 5);
box(gamewin, 0, 0);
refresh();
wrefresh(gamewin);
// draw roads
int road_width = (width - 2) / amount_roads;
for (int i = road_width; i < width - 2; i += road_width + 1) {
for (int j = 1; j < height - 1; j ++) {
mvwprintw(gamewin, j, i, "|");
}
}
//draw car
char car[8][12] =
{
{' ', ' ', ' ', '_', '_', '_', '_', '_', ' ', ' ', ' ', '\0'},
{' ', '(', ')', ' ', '|', '|', '|', ' ', '(', ')', ' ', '\0'},
{'/', ' ', '_', '_', '_', '_', '_', '_', '_', ' ', '\\', '\0'},
{'|', '/', '_', '_', '_', '_', '_', '_', '_', '\\', '|', '\0'},
{'|', ' ', ' ', '_', '_', '_', '_', '_', ' ', ' ', '|', '\0'},
{'|', ' ', ' ', '\\', '_', '_','_', '/', ' ', ' ', '|', '\0'},
{'\\', '#', '#', ' ', '|', ' ', '|', ' ', '#', '#', '/', '\0'},
{' ', ' ', '#', '_', '|', '_', '|', '_', '#', ' ', ' ', '\0'}
};
int y = height - 9; //y position
for (int i = 0; i < 8; i ++) {
int x = (road_width/2) - (11/2); // x position
for (int j = 0; j < 12; j ++) {
mvwprintw(gamewin, y, x, "%c", car[i][j]);
x ++;
}
y ++;
}
wrefresh(gamewin);
// let car moves
void delete_car() {
int yCurr, xCurr;
getyx(gamewin, yCurr, xCurr);
int yPos = height - 9; //y position
for (int i = 0; i < 8; i ++) {
int xPos = xCurr - 13; // x position
for (int j = 0; j < 12; j ++) {
mvwprintw(gamewin, yPos, xPos, "%c", ' ');
xPos ++;
}
yPos ++;
}
wrefresh(gamewin);
}
int move_car(const char direction) {
// let to move at given side
int add;
if (direction == 'l') add = -road_width;
else if (direction == 'r') add = road_width;
else return 0;
//defines a start position for car from left side
int yCurr, xCurr;
getyx(gamewin, yCurr, xCurr);
int yPos = height - 9;
for (int i = 0; i < 8; i ++) {
int xPos = (xCurr - 13) + add; // x position
for (int j = 0; j < 12; j ++) {
mvwprintw(gamewin, yPos, xPos, "%c", car[i][j]);
xPos ++;
}
yPos ++;
}
wrefresh(gamewin);
getyx(gamewin, yCurr, xCurr);
return xCurr;
}
int attempt = 0;
while (attempt < 20) {
keypad(gamewin, TRUE);
int move = mvwgetch(gamewin, y - 1, ((road_width/2) - (11/2)) + 13);
if (move == KEY_RIGHT) {
int yCurr, xCurr; // check is the road on the right
getyx(gamewin, yCurr, xCurr);
if ((xCurr - 13) > (road_width * amount_roads + (amount_roads - 1))) {
wprintw(gamewin, "There is no the road");
}
delete_car();// clear current road
move_car('r');// add car to next road
} else if (move == KEY_LEFT) {
int yCurr, xCurr; // check is the road on the left
getyx(gamewin, yCurr, xCurr);
if ((xCurr - 13) < road_width) wprintw(gamewin, "There is no the road");
delete_car(); // clear current road
move_car('l'); // add car to next road
}
}
wrefresh(gamewin);
getch();
endwin();
return EXIT_SUCCESS;
}
I'm writing a small game using ncurses, first I create a user login window, then a window for the game itself. Literally yesterday, everything was working fine, today nothing is displayed except for the standard window with the input at the very beginning. I don't understand what the matter is, maybe I accidentally deleted something, I don't know. I have read the entire code several times, and I have not found where the error is, please tell me what is wrong
A single function can be used to erase or print the car.
Use the xMax and yMax as a base for the window dimensions so the window fits in the terminal.
#include <stdlib.h>
#include <ncurses.h>
#include <time.h>
#define CAR_HEIGHT 8
#define CAR_WIDTH 12
void draw_car ( WINDOW *gamewin, char car[][CAR_WIDTH], int y, int x, int show) {
for ( int i = 0; i < CAR_HEIGHT; i ++) {
for ( int j = 0; j < CAR_WIDTH; j ++) {
if ( show) {
mvwprintw ( gamewin, y + i, x + j, "%c", car[i][j]);
} else {
mvwprintw ( gamewin, y + i, x + j, " ");
}
}
}
}
int main ( void) {
initscr ( );
cbreak ( );
noecho ( );
srand ( time ( NULL));
// get screen sizes
int yMax = 0, xMax = 0;
getmaxyx ( stdscr, yMax, xMax);
//create a new window for input
int height = yMax - 2;
int width = xMax - 2;
WINDOW *inputwin = newwin ( height, width, 1, 1);
box ( inputwin, 0, 0);
refresh ( );
wrefresh ( inputwin);
// get amount roads
echo ( );
mvwprintw ( inputwin, 1, 1, "Press key left to choose beginner level.");
mvwprintw ( inputwin, 2, 7, "key right - intermediate.");
mvwprintw ( inputwin, 3, 7, "key up - advanced.");
int amount_roads = 0;
keypad ( inputwin, TRUE);
int a = 0;
do {
a = mvwgetch ( inputwin, 7, 60);
switch ( a) {
case KEY_LEFT:
mvwprintw ( inputwin, 5, width / 2, "You chose beginner ");
amount_roads = 2;
break;
case KEY_RIGHT:
mvwprintw ( inputwin, 5, width / 2, "You chose intermediate ");
amount_roads = 3;
break;
case KEY_UP:
mvwprintw ( inputwin, 5, width / 2, "You chose advanced ");
amount_roads = 4;
break;
case KEY_DOWN:
break;
default:
amount_roads = 0;
}
if ( amount_roads) {
mvwprintw ( inputwin, 6, width / 2, "Press key down to continue");
} else {
mvwprintw ( inputwin, 5, width / 2, "INCORRECT INPUT. TRY AGAIN");
mvwprintw ( inputwin, 6, width / 2, " ");
}
wrefresh ( inputwin);
} while ( ! amount_roads || a != KEY_DOWN);
wrefresh ( inputwin);
delwin ( inputwin);
refresh ( );
//create win for game
height = yMax - 4;
width = xMax - 4;
WINDOW *gamewin = newwin ( height, width, 2, 2);
box ( gamewin, 0, 0);
keypad ( gamewin, TRUE);
refresh ( );
wrefresh ( gamewin);
// draw roads
int road_width = ( width - amount_roads) / amount_roads;
for ( int i = road_width; i < width - 2; i += road_width + 1) {
for ( int j = 2; j < height - 1; j ++) {
mvwprintw ( gamewin, j, i, "|");
}
}
char car[CAR_HEIGHT][CAR_WIDTH] = {
{ " _____ "},
{ " () ||| () "},
{ "/ _______ \\"},
{ "|/_______\\|"},
{ "| _____ |"},
{ "| \\___/ |"},
{ "\\## | | ##/"},
{ " #_|_|_# "}
};
int y = height - 9; //y position
int road_lane = 0;
int show = 1;
int x = road_lane * road_width + ( road_width / 2 - 5); // x position
draw_car ( gamewin, car, y, x, show);
wrefresh ( gamewin);
int attempt = 0;
int change = 0;
do {
change = mvwgetch ( gamewin, 1, 1);
draw_car ( gamewin, car, y, x, ! show);
switch ( change) {
case KEY_RIGHT:
if ( road_lane == amount_roads - 1) {
mvwprintw ( gamewin, 1, 2, "There is no the road");
} else {
mvwprintw ( gamewin, 1, 2, " ");
++road_lane;
x += road_width + 1; // x position
}
break;
case KEY_LEFT:
if ( road_lane == 0) {
mvwprintw ( gamewin, 1, 2, "There is no the road");
} else {
mvwprintw ( gamewin, 1, 2, " ");
--road_lane;
x -= road_width + 1; // x position
}
break;
case KEY_DOWN:
if ( y == height - 9) {
mvwprintw ( gamewin, 1, 2, "There is no the road");
} else {
mvwprintw ( gamewin, 1, 2, " ");
++y;
}
break;
case KEY_UP:
if ( y == 2) {
mvwprintw ( gamewin, 1, 2, "There is no the road");
} else {
mvwprintw ( gamewin, 1, 2, " ");
--y;
}
break;
}
draw_car ( gamewin, car, y, x, show);
} while ( attempt < 20 && change != '\n');
wrefresh ( gamewin);
// getch();
delwin ( gamewin);
endwin ( );
return EXIT_SUCCESS;
}

why does a call of a function with set parameters does not generate the expected output?

In the following code, when I debugged it, the first call function (mazesentence(0, 0, 1, 0);) also went to the if(before == 1 ), but 'before' is clearly a zero. Why is it entering in that location? I would imagine, i have my braces incorrectly.
#include <stdio.h>
void mazesentence(int little, int twist, int twisting, int before)
{
if(little == 0 && twist == 0 && twisting == 1)
{
if(before == 0)
{
if(twisting == 0)
printf("twisty ");
else if(twisting == 1)
printf("twisting ");
printf("little ");
printf("maze of ");
}
if(before == 1)
printf("little ");
{
if(twisting == 0)
printf("twisty ");
else if(twisting == 1)
printf("twisting ");
printf("maze of");
}
}
return;
}
int main( )
{
mazesentence(0, 0, 1, 0);
mazesentence(0, 0, 1, 1);
mazesentence(0, 0, 0, 0);
mazesentence(0, 0, 0, 1);
mazesentence(0, 1, 1, 0);
mazesentence(0, 1, 0, 0);
mazesentence(1, 0, 1, 0);
mazesentence(1, 0, 0, 0);
mazesentence(1, 1, 1, 0);
mazesentence(1, 1, 1, 1);
mazesentence(1, 1, 0, 0);
mazesentence(1, 1, 0, 1);
return 0;
}
Your braces are indeed incorrect. Looking at the problem conditional:
if(before == 1)
printf("little ");
{
The way this is, the if only guards the printf("little "). That is then is followed by a code block that executes unconditionally.
It should be this to guard the entire block:
if(before == 1)
{
printf("little ");

Hamiltonian Cycle with fork

hellow friends
i want to write Hamiltonian Cycle for 5 vertexes with fork. and i want to create a process for each vertex to check is it true vertex in (hamCycleUtil function )or no. so i write this code but it has false output.i cant solve the problem .pleas help.how can i write this code true ?i just want create 5 process to check the vertexes.
/*
* C Program to Find Hamiltonian Cycle
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include<sys/types.h>
#include<unistd.h>
#define V 5
void printSolution(int path[]);
/*
* check if the vertex v can be added at index 'pos' in the Hamiltonian Cycle
*/
bool isSafe(int v, bool graph[V][V], int path[], int pos)
{
if (graph [path[pos-1]][v] == 0)
return false;
for (int i = 0; i < pos; i++)
if (path[i] == v)
return false;
return true;
}
/* solve hamiltonian cycle problem */
bool hamCycleUtil(bool graph[V][V], int path[], int pos)
{
int pid;
int s=-1;//counter
if (pos == V)
{
if (graph[ path[pos-1] ][ path[0] ] == 1)
return true;
else
return false;
}
for (int v = 1; v < V; v++)
{
pid=fork();
s++;
if(s<V)// to control the number of fork
{
if(v==1)
{
pid=1;}
pid=fork();
s++;
if(pid < 0) {
printf("Error");
}
else if (pid == 0){
if (isSafe(v, graph, path, pos))
{
path[pos] = v;
if (hamCycleUtil (graph, path, pos+1) == true)
return true;
path[pos] = -1;
}
}
else {
if (isSafe(v, graph, path, pos))
{
path[pos] = v;
if (hamCycleUtil (graph, path, pos+1) == true)
return true;
path[pos] = -1;
}
}
}
}
return false;
}
/* solves the Hamiltonian Cycle problem using Backtracking.*/
bool hamCycle(bool graph[V][V])
{
int *path = malloc(V*sizeof(int));
for (int i = 0; i < V; i++)
path[i] = -1;
path[0] = 0;
if (hamCycleUtil(graph, path, 1) == false)
{
printf("\nSolution does not exist");
return false;
}
printSolution(path);
return true;
}
/* Main */
void printSolution(int path[])
{
printf("Solution Exists:");
printf(" Following is one Hamiltonian Cycle \n");
for (int i = 0; i < V; i++)
printf(" %d",path[i]);
printf(" %d",path[0]);
}
int main()
{
/* Let us create the following graph
(0)--(1)--(2)
| / \ |
| / \ |
| / \ |
(3)-------(4) */
bool graph1[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0},
};
{ hamCycle(graph1);
/* Let us create the following graph
(0)--(1)--(2)
| / \ |
| / \ |
| / \ |
(3) (4) */
bool graph2[V][V] = {{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 0},
{0, 1, 1, 0, 0},
};
hamCycle(graph2);
return 0;
}}

Arduino Esplora Pong Game

Over the last couple of days I've been working on a Pong game for Arduino Esplora with Arduino's TFT display. Everything in the game works except that, when the player scores the ball gets deleted from it's last location and reappears in the center (as it should), while when the computer scores the ball doesn't get deleted (or overwritten would be a better word) but reappears in the center. I have tried some changes on this area of the code with no success, particularly since I have no idea where it comes from. Goal detection goes from line 161 to 187.
#include <Esplora.h>
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
float BPX;
float BPY;
int byx;
int bx;
int A;
int by = 1;
int playerScore;
int computerScore;
#define WINSCORE 5
int CPaddlePlus;
int CPaddleMinus;
int R, L, D, U;
int playerPaddle;
int computerPaddle;
int Random;
void setup() {
// initialize the screen
EsploraTFT.begin();
EsploraTFT.background(0, 0, 0);
EsploraTFT.setTextColor(ST7735_YELLOW, ST7735_BLACK);
EsploraTFT.setTextSize(5);
EsploraTFT.setCursor(22, 15);
EsploraTFT.print("PONG");
EsploraTFT.setTextSize(1);
EsploraTFT.println("");
EsploraTFT.println(" By: David Rutherford");
EsploraTFT.println("");
EsploraTFT.println(" Esplora port by:");
EsploraTFT.println(" -Mike Barela");
EsploraTFT.println(" -Bernardo Meurer");
EsploraTFT.setTextColor(ST7735_WHITE, ST7735_BLACK);
EsploraTFT.println(" ");
EsploraTFT.println(" Press Switch 4 To Start");
while (Esplora.readButton(SWITCH_RIGHT) == HIGH)
;
EsploraTFT.fillScreen(ST7735_BLACK);
EsploraTFT.setRotation(0);
DrawCourt(0);
playerScore = 0;
computerScore = 0;
DisplayScore(playerScore, computerScore);
BPX = 15;
BPY = 15;
byx = 15;
bx = 1;
A = 1;
playerPaddle = 48;
computerPaddle = 48;
long seed = Esplora.readLightSensor() * Esplora.readMicrophone() / Esplora.readTemperature(DEGREES_F);
randomSeed(seed);
}
void loop() {
if ((BPY == 80) || (BPY == 20)) {
Random = random(1, 10);
}
CPaddlePlus = computerPaddle + 16;
CPaddleMinus = computerPaddle - 16;
if (Random <= 8) {
if ((A == 1) || ((BPY > 100) && (A == -1))) {
if ((bx == -1) && (BPX < (CPaddlePlus))) {
U = 1;
D = 0;
}
if ((bx == 1) && (BPX > (CPaddlePlus))) {
D = 1;
U = 0;
}
}
else {
D = 0;
U = 0;
}
}
if ((Random > 8) && (Random <= 9)) {
if ((A == 1) || ((BPY > 100) && (A == -1))) {
if ((bx == -1) && (BPX < (CPaddlePlus))) {
U = 0;
D = 1;
}
if ((bx == 1) && (BPX > (CPaddlePlus))) {
D = 0;
U = 1;
}
}
else {
D = 0;
U = 0;
}
}
if (Random > 9) {
if ((A == 1) || ((BPY > 100) && (A == -1))) {
if ((bx == -1) && (BPX < (CPaddleMinus))) {
U = 1;
D = 0;
}
if ((bx == 1) && (BPX > (CPaddleMinus))) {
D = 1;
U = 0;
}
}
else {
D = 0;
U = 0;
}
}
DrawCourt(0);
R = Esplora.readButton(SWITCH_DOWN);
L = Esplora.readButton(SWITCH_UP);
playerPaddle = playerPaddle + R;
playerPaddle = playerPaddle - L;
computerPaddle = computerPaddle + D;
computerPaddle = computerPaddle - U;
EsploraTFT.fillRect(playerPaddle - 1, 3, 2, 3, ST7735_BLACK);
EsploraTFT.fillRect(playerPaddle + 33, 3, 2, 3, ST7735_BLACK);
EsploraTFT.fillRect(playerPaddle, 3, 32, 3, ST7735_GREEN);
if (playerPaddle == 1) {
playerPaddle = 2;
}
if (playerPaddle == 95) {
playerPaddle = 94;
}
EsploraTFT.fillRect(computerPaddle, 154, 32, 3, ST7735_GREEN);
EsploraTFT.fillRect(computerPaddle - 1, 154, 2, 3, ST7735_BLACK);
EsploraTFT.fillRect(computerPaddle + 33, 154, 2, 3, ST7735_BLACK);
if (computerPaddle == 1) {
computerPaddle = 2;
}
if (computerPaddle == 95) {
computerPaddle = 94;
}
byx += A;
BPY = byx;
BPX += bx ;
if ((BPX == 127) || (BPX == 2)) {
(bx = (-1 * bx));
}
else {
};
if ((BPX <= (computerPaddle + 38)) && (BPX >= (computerPaddle - 6)) && (BPY == 149)) {
(A = (-1 * A));
}
else {
};
if ((BPX <= (playerPaddle + 38) && (BPX >= (playerPaddle - 6)) && (BPY == 11))) {
(A = (-1 * A));
}
else {
};
if (BPY >= 160 || BPY <= 0) {//Goal Detection
if (BPY >= 160) {
playerScore = playerScore + 1;
DisplayScore(playerScore, computerScore);
DrawCourt(0);
EsploraTFT.fillCircle(BPX, BPY, 7, ST7735_BLACK);
BPX = 64;
BPY = 80;
EsploraTFT.fillCircle(BPX, BPY, 4, ST7735_GREEN);
delay(3000);
EsploraTFT.fillCircle(BPX, BPY, 7, ST7735_BLACK);
byx = 80;
}
else {
computerScore = computerScore + 1;
DisplayScore(playerScore, computerScore);
DrawCourt(0);
EsploraTFT.fillCircle(BPX, BPY, 7, ST7735_BLACK);
BPX = 64;
BPY = 80;
EsploraTFT.fillCircle(BPX, BPY, 4, ST7735_GREEN);
delay(3000);
EsploraTFT.fillCircle(BPX, BPY, 7, ST7735_BLACK);
byx = 80;
}
}
DisplayScore(playerScore, computerScore);
if (playerScore == WINSCORE || computerScore == WINSCORE) {
EsploraTFT.setRotation(1);
EsploraTFT.setTextColor(ST7735_WHITE, ST7735_BLACK);
EsploraTFT.setCursor(8, 50);
EsploraTFT.setTextSize(2);
if (playerScore == WINSCORE) {
EsploraTFT.print("YOU WIN");
}
else {
EsploraTFT.print("ESPLORA WINS");
}
EsploraTFT.setTextSize(1);
EsploraTFT.setTextColor(ST7735_YELLOW, ST7735_BLACK);
EsploraTFT.setCursor(8, 90);
EsploraTFT.print("Press Switch 4 To Restart");
while (Esplora.readButton(SWITCH_RIGHT) == HIGH)
;
EsploraTFT.setRotation(0);
EsploraTFT.fillScreen(ST7735_BLACK);
EsploraTFT.fillCircle(BPX, BPY, 7, ST7735_BLACK);
BPX = 15;
BPY = 15;
byx = 15;
bx = 1;
A = 1;
EsploraTFT.fillCircle(BPX, BPY, 4, ST7735_GREEN);
EsploraTFT.fillCircle(BPX, BPY, 7, ST7735_BLACK);
//EsploraTFT.fillCircle(BPX, BPY, 4, ST7735_GREEN);
//EsploraTFT.fillCircle(BPX, BPY, 7, ST7735_BLACK);
computerScore = 0;
playerScore = 0;
DrawCourt(0);
DisplayScore(playerScore, computerScore);
delay(2000);
}
EsploraTFT.fillCircle(BPX, BPY, 7, ST7735_BLACK);
EsploraTFT.fillCircle(BPX, BPY, 4, ST7735_GREEN);
}
void DrawCourt(boolean onlycenter) {
if (!onlycenter) {
EsploraTFT.drawFastVLine(0, 0, 160, ST7735_GREEN);
EsploraTFT.drawFastVLine(127, 0, 160, ST7735_GREEN);
}
EsploraTFT.drawFastHLine(0, 80, 127, ST7735_GREEN);
}
void DisplayScore(int playerScore, int computerScore) {
EsploraTFT.setRotation(1);
EsploraTFT.setTextColor(ST7735_GREEN, ST7735_BLACK);
EsploraTFT.setCursor(65, 5);
EsploraTFT.setTextSize(2);
EsploraTFT.print(playerScore);
EsploraTFT.setCursor(85, 5);
EsploraTFT.print(computerScore);
EsploraTFT.setRotation(0);
}
A visualization of what's happening:
So following #PaulOgilvie's idea I fixed the problem with the following code:
if (BPY >= 160 || BPY <= 0) {//Goal Detection
if (BPY >= 160) {
playerScore = playerScore + 1;
}
else {
computerScore = computerScore + 1;
}
DrawCourt(0);
EsploraTFT.fillCircle(BPX, BPY, 7, ST7735_BLACK);
EsploraTFT.fillRect(computerPaddle, 154, 32, 3, ST7735_GREEN);
EsploraTFT.fillRect(1,0,126,15,ST7735_BLACK);
EsploraTFT.fillRect(playerPaddle, 3, 32, 3, ST7735_GREEN);
DisplayScore(playerScore, computerScore);
BPX = 64;
BPY = 80;
EsploraTFT.fillCircle(BPX, BPY, 4, ST7735_GREEN);
delay(3000);
EsploraTFT.fillCircle(BPX, BPY, 7, ST7735_BLACK);
byx = 80;
}
Basically I just made a workaround drawing a big black rectangle on top of the whole thing.

Find intersection in 2d arrays

I wrote a small code to find intersection of two 2d array, unfortunately it is not working, so maybe you can help me.. Intersection is, if both numbers on place (x,y) is a "1". Otherwise there should be "0"
void intersection(int *mat, int rows, int cols) {
int rows1 = 5, cols1 = 4; // secend matrix is in function because i just need it here
int ma2[] = { 0, 0, 1, 0, 1, // 1. Zeile
0, 0, 1, 0, 1, // 2. Zeile
0, 0, 1, 1, 0, // 3. Zeile
0, 0, 1, 0, 0 // 4. Zeile
};
int i = 0;
int j = 0;
int x = 0;
int y = 0;
while (j < cols && y < cols1) { // maybe it is better with a for loop ??
j += 1;
y += 1;
while (i < rows && x < rows1) {
i += 1;
x += 1;
if (mat[j*rows+i] == 1 && ma2[y*rows1+x] == 1) {
printf("%d ", mat[j*rows+i]);
break;
} else {
printf("%d ", mat[j*rows+i]);
break;
}
}
printf("\n");
}
}
int main (void) {
int rows = 5, cols = 4; //first matrix is in main, because i need it for other functions
int ma[] = { 0, 0, 1, 0, 0, // 1. Zeile
1, 0, 0, 0, 0, // 2. Zeile
1, 0, 1, 0, 0, // 3. Zeile
0, 0, 1, 0, 0 // 4. Zeile
};
intersection(ma, rows, cols);
return 0;
}
Output should be (in this case):
{ 0, 0, 1, 0, 0, // 1. Zeile
0, 0, 0, 0, 0, // 2. Zeile
0, 0, 1, 0, 0, // 3. Zeile
0, 0, 1, 0, 0 // 4. Zeile
};
but i just get a matrix with 1 row
Greets ;)
try this
#define min(x,y) ((x) < (y) ? (x) : (y))
void intersection(int *mat, int rows, int cols) {
rows = min(rows, 5);//rows <--> cols
cols = min(cols, 4);
int ma2[] = { 0, 0, 1, 0, 1, // 1. Zeile
0, 0, 1, 0, 1, // 2. Zeile
0, 0, 1, 1, 0, // 3. Zeile
0, 0, 1, 0, 0 // 4. Zeile
};
int i, j;
for(i = 0; i < cols; ++i){
for(j = 0; j < rows; ++j){
//printf("%d ", mat[i*rows + j] == ma2[i*rows + j] ? mat[i*rows + j] : 0);
printf("%d ", mat[i*rows + j] & ma2[i*rows + j]);
}
printf("\n");
}
}
I solved the problem with this solution ;) Thank you everyone for helping me ...
void intersection(int *mat, int rows, int cols) {
int ma2[4][5] = {{0, 1, 0, 0, 0},
{0, 1, 0, 0, 0},
{1, 1, 0, 1, 0},
{0, 1, 0, 0, 0}};
int i = 0;
int j = 0;
int t = 1;
int s = 0;
for(j = 0; j < cols; j++) {
for (i = 0; i < rows; i++) {
if (ma2[j][i] && mat[j*rows+i] == 1) {
printf("%d ", t);
} else {
printf("%d ", s);
}
}
printf("\n");
}
}

Resources