Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7051789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:18:20+00:00 2026-05-28T03:18:20+00:00

I write a client for a console game, 1vs1. In the game one player

  • 0

I write a client for a console game, 1vs1. In the game one player have to catch the other, and every player is rappresented with a char, printed on the console. I use the mvaddch() to delete the old position, and to print the new position on the console.
My code generate 2 process:

  • Process A: It get the input from the keyboard and update the position on the screen;
  • Process B: it get the input from the server and update the position of the enemy on the screen;

My problem is the old position of the enemy is not deleted (overwriting with ‘ ‘), and so process B generates a snake of charactes on the screen. Process A work good.

  initscr();
noecho();
curs_set(0);
  //process A is created now
switch ( pid = fork() ) {
    case -1:    perror("fork() fallita");   exit(0);
    case 0:     {char c;    struct pos old_position = {welcome_position.c, welcome_position.x, welcome_position.y};
                    struct pos position = {welcome_position.c, welcome_position.x, welcome_position.y};
                    mvaddch(position.y, position.x, position.c);
            while (1) {
                switch(c=getch()) {
                    case SU:    if(position.y>0) {  position.y-=1; } break;
                    case GIU:   if(position.y<MAXY-1){  position.y+=1; } break;
                    case SINISTRA:  if(position.x>0){   position.x-=1; } break;
                    case DESTRA:    if(position.x<MAXX-1){  position.x+=1; } break;
                    default: break;     }
                if ((position.x != old_position.x) || (position.y != old_position.y)) { 
                    send(sock, &position, sizeof(struct pos), 0);
                    mvaddch(old_position.y, old_position.x, ' ');
                    mvaddch(position.y, position.x, position.c);
                    refresh();
                    old_position.x = position.x;
                    old_position.y = position.y; }}     }   
    default:    break ;         }                   
   // Process B is here
struct pos position; 
struct pos old_position={' ', -1,-1};
    while (1) {
        while ( recv(sock, &position, sizeof(struct pos), 0) < 1 )
            mvaddch(old_position.y, old_position.x, ' '); // THE PROBLEM
            mvaddch(position.y, position.x, position.c); // Works => snake
            refresh();
            old_position.x = position.x;
            old_position.y = position.y;}   
endwin();
kill(pid);
printf("\n-----------------------------\n");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:18:21+00:00Added an answer on May 28, 2026 at 3:18 am

    If you don’t want the entire trail showing, you have to keep a record of the previous position of each character (player) and arrange to write a blank at the old position and the correct mark at the new position. If you’re feeling fancy, you might use a coloured blank, one colour for each player, so you can see where each has been, even though the current positions are marked differently.

    Unfortunately, without the colouration mentioned, that looks like what you’re doing.

    You should make sure you don’t use the -1 coordinates; mvaddch() is probably not error checked and will go trampling out of bounds, doing who knows what damage. Don’t risk it. (Consider using 0, 0 as the old position; it won’t matter if you write a blank over a blank. The only thing that matters is that the other player is not where you write the blank.)

    Note that it is crucial that only one process is doing the drawing. If you have two processes attempting to do so, you will lose one or the other images some of the time. This is one reason reason why it is hard to add a clock, say, to the top right corner of a terminal screen. You do seem to have two processes trying to write to the screen.

    Style-wise, you need to use more functions. That is, the different processes should each have their code in a separate function, so that it is easier to see what they are doing. Stacking close braces } three deep on a single line is not good style either.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a client for a easy game console, 1vs1. The game:
For one specific client I have to use PHP. This and this question were
I have a situation where I have to write a client in Java against
I'm trying to write a client-server console application on C# using UDP. And i
I have two classes; one is a server, another one is a client, and
I have 2 important classes(client and server) and I will write something in my
I have application that has to write to console. Also I call function from
I am trying to use curly braces in mongodb native console client on Windows
I wrote some sort of console client for a simple application. To be more
I start to write a client - server application using .net (C#) for both

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.