Added all previous files
This commit is contained in:
parent
4466ee8a4c
commit
f04fb2189a
59 changed files with 64921 additions and 0 deletions
14
lab7/Life/Makefile
Normal file
14
lab7/Life/Makefile
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
|
||||
playlife: lifefunc.o playlife.o
|
||||
gcc lifefunc.o playlife.o -o playlife
|
||||
|
||||
lifefunc.o: lifefunc.c lifefunc.h
|
||||
gcc -c lifefunc.c -o lifefunc.o
|
||||
|
||||
playlife.o: playlife.c lifefunc.h
|
||||
gcc -c playlife.c -o playlife.o
|
||||
|
||||
clean:
|
||||
rm *.o
|
||||
rm playlife
|
||||
|
||||
156
lab7/Life/lifefunc.c
Normal file
156
lab7/Life/lifefunc.c
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
// Author: Owen Dorweiler
|
||||
// Class: Fundementals of Computing
|
||||
// Assignment: Lab 7
|
||||
// Due: 5 November 2024
|
||||
|
||||
#include "lifefunc.h"
|
||||
|
||||
void initBoard(char board[][SIZE]) {
|
||||
for (int y = 0; y < SIZE; y++) {
|
||||
for (int x = 0; x < SIZE; x++) {
|
||||
board[x][y] = ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void interactiveMode(char board[][SIZE]) {
|
||||
char action;
|
||||
int xCoor, yCoor;
|
||||
|
||||
// Print blank board and ask for first command
|
||||
printBoard(board);
|
||||
printf("COMMAND: ");
|
||||
scanf(" %c", &action);
|
||||
|
||||
while (action != 'q') {
|
||||
switch (action) {
|
||||
case 'a':
|
||||
// Read coordinates, add point, print board
|
||||
scanf(" %d %d", &xCoor, &yCoor);
|
||||
board[xCoor][yCoor] = 'X';
|
||||
printBoard(board);
|
||||
break;
|
||||
case 'r':
|
||||
// Read coordinates, remove point, print board
|
||||
scanf(" %d %d", &xCoor, &yCoor);
|
||||
board[xCoor][yCoor] = ' ';
|
||||
printBoard(board);
|
||||
break;
|
||||
case 'n':
|
||||
// Iterate once and print board
|
||||
iterateBoard(board);
|
||||
printBoard(board);
|
||||
break;
|
||||
case 'p':
|
||||
// Run game until user interrupts
|
||||
runGame(board);
|
||||
break;
|
||||
default:
|
||||
// Print error message
|
||||
printf("Unknown command. Please try again.");
|
||||
break;
|
||||
}
|
||||
|
||||
printf("COMMAND: ");
|
||||
scanf(" %c", &action);
|
||||
}
|
||||
}
|
||||
|
||||
void batchMode(char board[][SIZE], char *filename) {
|
||||
int xCoor, yCoor;
|
||||
|
||||
FILE *file = fopen(filename, "r");
|
||||
if (file == NULL) {
|
||||
printf("Error opening file. Please try again.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char command[10];
|
||||
while (1) {
|
||||
fgets(command, 10, file);
|
||||
|
||||
if (command[0] == 'a') {
|
||||
sscanf(command, "a %d %d", &xCoor, &yCoor);
|
||||
board[xCoor][yCoor] = 'X';
|
||||
}
|
||||
else if (command[0] == 'p')
|
||||
runGame(board);
|
||||
else {
|
||||
printf("No 'run' command in file. Exiting...\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printBoard(char board[][SIZE]) {
|
||||
// Clear the screen
|
||||
system("clear");
|
||||
|
||||
// Print top border
|
||||
for (int x = 0; x < SIZE + 2; x++) {
|
||||
printf("-");
|
||||
}
|
||||
|
||||
// Print board data
|
||||
for (int y = 0; y < SIZE; y++) {
|
||||
printf("\n|");
|
||||
for (int x = 0; x < SIZE; x++) {
|
||||
printf("%c", board[x][y]);
|
||||
}
|
||||
printf("|");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// Print bottom border
|
||||
for (int x = 0; x < SIZE + 2; x++) {
|
||||
printf("-");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void iterateBoard(char board[][SIZE]) {
|
||||
int cpyBrd[SIZE][SIZE];
|
||||
int neighborSum;
|
||||
|
||||
// Copy the contents of board to copyBrd
|
||||
for (int y = 0; y < SIZE; y++) {
|
||||
for (int x = 0; x < SIZE; x++) {
|
||||
cpyBrd[x][y] = board[x][y];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (int y = 0; y < SIZE; y++) {
|
||||
for (int x = 0; x < SIZE; x++) {
|
||||
neighborSum = 0;
|
||||
|
||||
// Sum 9 cells (including test cell)
|
||||
for (int j = y-1; j < y+2; j++) {
|
||||
for (int i = x-1; i < x+2; i++) {
|
||||
if (i >= 0 && j >= 0)
|
||||
if (cpyBrd[i][j] == 'X')
|
||||
neighborSum++;
|
||||
}
|
||||
}
|
||||
|
||||
// Subtract test cell from neighborSum
|
||||
if (board[x][y] == 'X')
|
||||
neighborSum--;
|
||||
|
||||
// If the cell is dead and has 3 neighbors, make it alive
|
||||
if (board[x][y] == ' ' && neighborSum == 3)
|
||||
board[x][y] = 'X';
|
||||
// If the cell is alive and does not have 2 or 3 neighbors, kill it
|
||||
else if (board[x][y] == 'X' && (neighborSum < 2 || neighborSum > 3))
|
||||
board[x][y] = ' ';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void runGame(char board[][SIZE]) {
|
||||
while (1) {
|
||||
iterateBoard(board);
|
||||
printBoard(board);
|
||||
usleep(100000);
|
||||
}
|
||||
}
|
||||
17
lab7/Life/lifefunc.h
Normal file
17
lab7/Life/lifefunc.h
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// Author: Owen Dorweiler
|
||||
// Class: Fundementals of Computing
|
||||
// Assignment: Lab 7
|
||||
// Due: 5 November 2024
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define SIZE 40
|
||||
|
||||
void initBoard(char board[][SIZE]);
|
||||
void interactiveMode(char board[][SIZE]);
|
||||
void batchMode(char board[][SIZE], char *filename);
|
||||
void printBoard(char board[][SIZE]);
|
||||
void iterateBoard(char board[][SIZE]);
|
||||
void runGame(char board[][SIZE]);
|
||||
BIN
lab7/Life/lifefunc.o
Normal file
BIN
lab7/Life/lifefunc.o
Normal file
Binary file not shown.
18
lab7/Life/myscene.txt
Normal file
18
lab7/Life/myscene.txt
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
a 10 10
|
||||
a 10 11
|
||||
a 10 12
|
||||
a 30 10
|
||||
a 30 11
|
||||
a 31 10
|
||||
a 31 11
|
||||
a 10 15
|
||||
a 11 16
|
||||
a 9 17
|
||||
a 10 17
|
||||
a 11 17
|
||||
a 5 35
|
||||
a 6 35
|
||||
a 6 34
|
||||
a 6 36
|
||||
a 7 36
|
||||
p
|
||||
BIN
lab7/Life/playlife
Executable file
BIN
lab7/Life/playlife
Executable file
Binary file not shown.
22
lab7/Life/playlife.c
Normal file
22
lab7/Life/playlife.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// Author: Owen Dorweiler
|
||||
// Class: Fundementals of Computing
|
||||
// Assignment: Lab 7
|
||||
// Due: 5 November 2024
|
||||
|
||||
#include "lifefunc.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
char board[SIZE][SIZE];
|
||||
initBoard(board);
|
||||
|
||||
if (argc == 1)
|
||||
// Enter interactive mode
|
||||
interactiveMode(board);
|
||||
else if (argc == 2) {
|
||||
batchMode(board, argv[1]);
|
||||
}
|
||||
else printf("Too many arguments. Please try again.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
lab7/Life/playlife.o
Normal file
BIN
lab7/Life/playlife.o
Normal file
Binary file not shown.
Loading…
Reference in a new issue