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.
46
lab7/report.txt
Normal file
46
lab7/report.txt
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
Author: Owen Dorweiler
|
||||
Class: Fundementals of Computing
|
||||
Assignment: Lab 7 Report
|
||||
Due: 5 November 2024
|
||||
|
||||
USER PERSPECTIVE:
|
||||
|
||||
If the user runs the program without any command line arguments, the program enters interactive mode, displaying a blank board and allowing the user to add points using the "a [x] [y]" command, remove using the "r [x] [y]" command, iterate once by typing "n", and run continuously by typing "p". Every time the user adds a point, removes a point, or iterates, the program updates the board and asks for another command. When the user types "p", the program iterates until the user interrupts it by typing Ctrl + C. If the user types something other than one of these commands, the program ouputs an error.
|
||||
|
||||
If the user runs the program with a file name, the program iterates continuously using the data from that file. If the file cannot be found, the program gives an error message. If the program does not have a "p" command, the program also gives an error message.
|
||||
|
||||
INTERNALLY:
|
||||
|
||||
The program first initializes the board by calling the initBoard() function from main(). This function just fills the 40x40 board with spaces.
|
||||
|
||||
The program then checks to see how many command line arguments there are. If there is only one, it calls the interactiveMode() function, if there are two, it calls the batchMode() function, and if there are more than two it gives an error message.
|
||||
|
||||
Interactive Mode
|
||||
|
||||
The interactiveMode() function first prints the board by calling the printBoard() function, which first clears the screen, then prints the board inside a border. Then the user is prompted for a command. The interactiveMode() function reads in the first letter of this command, enters a while loop as long as the command isn't 'q' to quit, and performs a switch statement on it:
|
||||
|
||||
1) If the command is "a", it reads in the two points from the buffer, adds that point to the board, and prints the board using printBoard().
|
||||
|
||||
2) If the command is "r", it reads in the two points from the budder, removes that point from the board, and prints the board using printBoard().
|
||||
|
||||
3) If the command is "n", it calls iterateBoard(), which iterates the board by one. iterateBoard() works by using a nested for loop to go through each point on the board, adding up how many Xs are around it (using another nested for loop), and making a cell alive if it has exactly 3 live cells bordering it, or killing a living cell if it has less than 2 or more than 3 living cells surrounding it.
|
||||
|
||||
4) If the command is "p", the program calls runGame(), which loops forever, calling iterateBoard() then printBoard() then sleeping for 0.1 seconds, then repeating.
|
||||
|
||||
5) If the command starts with something else, an error message is given.
|
||||
|
||||
At the end of the switch statement, the program asks for a command again and loops back to the beginning of the while loop.
|
||||
|
||||
Batch Mode
|
||||
|
||||
The batchMode() function first opens the file using the name given as a command line argument. If the file does not exist, it gives an error and exits.
|
||||
|
||||
If the file does exist, it reads it line by line, and if the line starts with 'a', it reads in the coordinates and adds them to the board. If the line does not start with 'a', it checks if it starts with 'p'. If so, it calls the runGame() command to start the continuous iteration. If not, it outputs an error that no 'run' command was found in the file, and exits.
|
||||
|
||||
OUTPUT VERIFICATION
|
||||
|
||||
I verified the output by checking it against a bunch of different cases. I checked all the possible errors and it worked as expected.
|
||||
|
||||
MYSCENE FILE
|
||||
|
||||
I used https://conwaylife.com/ to test various cell configurations to see what they would do, before creating them in the myscene.txt file. I also used the glider from the glider.txt file.
|
||||
Loading…
Reference in a new issue