Court and Player Number

This commit is contained in:
2026-04-24 15:44:16 -04:00
commit fdc6dd2004
5 changed files with 150 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
*.o
netpong
netpong.dSYM/

32
Makefile Normal file
View File

@@ -0,0 +1,32 @@
# Compiler and flags
CC = gcc
CFLAGS = -Wall -g
NETPONG_SRC = main.c court.c $(wildcard network/*.c)
LIBS = -lncurses
# Object files
NETPONG_OBJ = $(NETPONG_SRC:.c=.o)
# Default target
all: netpong
# Build NetPong
netpong: $(NETPONG_OBJ)
$(CC) $(CFLAGS) -o netpong $(NETPONG_OBJ) $(LIBS)
# Generic compile rule
%.o: %.c smsh.h varlib.h
$(CC) $(CFLAGS) -c $<
# Clean up
clean:
rm -f *.o netpong network/*.o
Server:
./netpong
Client:
./netpong -c 127.0.0.1

33
court.c Normal file
View File

@@ -0,0 +1,33 @@
#include "game.h"
#include <curses.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void DrawWalls(struct GameData data)
{
for (int i = LEFT_COL; i <= RIGHT_COL; i++) {
move(TOP_ROW, i);
addch('=');
move(BOT_ROW, i);
addch('-');
}
int col = LEFT_COL;
if (data.playerNumber > 0)
col = RIGHT_COL;
for (int i = TOP_ROW; i <= BOT_ROW; i++) {
move(i, col);
addch('|');
}
refresh();
}
void DrawHeader(struct GameData data)
{
move(TOP_ROW - 2, LEFT_COL);
addch('P');
addch('0' + (data.playerNumber + 1));
}

22
game.h Normal file
View File

@@ -0,0 +1,22 @@
#ifndef GAME_H
#define GAME_H 1
// Setup game board
#define TOP_ROW 4
#define BOT_ROW 21
#define LEFT_COL 9
#define RIGHT_COL 70
struct GameData
{
int playerNumber;
char* domain;
int port;
};
void DrawWalls(struct GameData);
void DrawHeader(struct GameData);
#endif

60
main.c Normal file
View File

@@ -0,0 +1,60 @@
#include "game.h"
#include <curses.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/**
* Main Fucntion
*/
int main(int argc, char* argv[])
{
// Create GameData Object
struct GameData data;
// Detect Server or Client
switch (argc) {
case 2:
// Start Server
data.playerNumber = 0;
data.port = atoi(argv[1]);
break;
case 3:
// Start Client
data.playerNumber = 1;
data.domain = argv[1];
data.port = atoi(argv[2]);
break;
default:
data.playerNumber = 0;
printf("Please read the following usage\n");
printf("Server: %s <portnumber>\n", argv[0]);
printf("Client: %s <domain> <port>\n", argv[0]);
return 1;
}
srand(getpid());
// Setup Terminal
if (initscr() == NULL) {
perror("initscr failed");
exit(1);
}
noecho();
cbreak();
nodelay(stdscr, TRUE);
DrawWalls(data);
DrawHeader(data);
int socket_fd = 0;
while (1) {
int ch = getch();
if (ch == 'q')
break;
}
endwin();
}