Created
December 2, 2016 04:17
-
-
Save tylersloeper/8a46ada432c36dd70ba2c004b8c372c5 to your computer and use it in GitHub Desktop.
CS50 Pset03 (2 parts)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* fifteen.c | |
* | |
* Computer Science 50 | |
* Problem Set 3 | |
* | |
* Implements Game of Fifteen (generalized to d x d). | |
* | |
* Usage: fifteen d | |
* | |
* whereby the board's dimensions are to be d x d, | |
* where d must be in [DIM_MIN,DIM_MAX] | |
* | |
* Note that usleep is obsolete, but it offers more granularity than | |
* sleep and is simpler to use than nanosleep; `man usleep` for more. | |
*/ | |
#define _XOPEN_SOURCE 500 | |
#include <cs50.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
// constants | |
#define DIM_MIN 3 | |
#define DIM_MAX 9 | |
// dimensions | |
int d; | |
int i; | |
int j; | |
int n; | |
int horizontal; | |
int vertical; | |
int board[9][9]; | |
// prototypes | |
void clear(void); | |
void greet(void); | |
void init(void); | |
void draw(void); | |
bool move(int tile); | |
bool won(void); | |
int main(int argc, string argv[]) | |
{ | |
// ensure proper usage | |
if (argc != 2) | |
{ | |
printf("Usage: fifteen d\n"); | |
return 1; | |
} | |
// ensure valid dimensions | |
d = atoi(argv[1]); | |
if (d < DIM_MIN || d > DIM_MAX) | |
{ | |
printf("Board must be between %i x %i and %i x %i, inclusive.\n", | |
DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX); | |
return 2; | |
} | |
// open log | |
FILE* file = fopen("log.txt", "w"); | |
if (file == NULL) | |
{ | |
return 3; | |
} | |
// greet user with instructions | |
greet(); | |
// initialize the board | |
init(); | |
// accept moves until game is won | |
while (true) | |
{ | |
// clear the screen | |
clear(); | |
// draw the current state of the board | |
draw(); | |
// log the current state of the board (for testing) | |
for (int i = 0; i < d; i++) | |
{ | |
for (int j = 0; j < d; j++) | |
{ | |
fprintf(file, "%i", board[i][j]); | |
if (j < d - 1) | |
{ | |
fprintf(file, "|"); | |
} | |
} | |
fprintf(file, "\n"); | |
} | |
fflush(file); | |
// check for win | |
if (won()) | |
{ | |
printf("ftw!\n"); | |
break; | |
} | |
// prompt for move | |
printf("Tile to move: "); | |
int tile = GetInt(); | |
// quit if user inputs 0 (for testing) | |
if (tile == 0) | |
{ | |
break; | |
} | |
// log move (for testing) | |
fprintf(file, "%i\n", tile); | |
fflush(file); | |
// move if possible, else report illegality | |
if (!move(tile)) | |
{ | |
printf("\nIllegal move.\n"); | |
usleep(500000); | |
} | |
// sleep thread for animation's sake | |
usleep(500000); | |
} | |
// close log | |
fclose(file); | |
// success | |
return 0; | |
} | |
/** | |
* Clears screen using ANSI escape sequences. | |
*/ | |
void clear(void) | |
{ | |
printf("\033[2J"); | |
printf("\033[%d;%dH", 0, 0); | |
} | |
/** | |
* Greets player. | |
*/ | |
void greet(void) | |
{ | |
clear(); | |
printf("WELCOME TO GAME OF FIFTEEN\n"); | |
usleep(1000000); | |
} | |
/** | |
* Initializes the game's board with tiles numbered 1 through d*d - 1 | |
* (i.e., fills 2D array with values but does not actually print them). | |
*/ | |
void init(void) | |
{ | |
int boardmaxvalue = (d * d); | |
int n = 0; | |
//initialize a non zero value to every place in the array to avoid errors associated with empty array positions. | |
for (int i = 0; i < 9; i++) | |
{ | |
for (int j = 0; j < 9; j++) | |
{ | |
board[i][j] = 99; | |
} | |
} | |
//now with correct values for array scope | |
for (int i = 0; i < d; i++) | |
{ | |
for (int j = 0; j < d; j++) | |
{ | |
n = n + 1; | |
board[i][j] = (boardmaxvalue - n); | |
} | |
} | |
if (d % 2 == 0) | |
{ | |
board[d-1][d-2] = 2; | |
board[d-1][d-3] = 1; | |
} | |
} | |
/** | |
* Prints the board in its current state. | |
*/ | |
void draw(void) | |
{ | |
for (int i = 0; i < d; i++) | |
{ printf("\n|||"); | |
for (int j = 0; j < d; j++) | |
{ | |
if (board[i][j] > 0) | |
{ | |
printf("\t%2i", board[i][j]); | |
} | |
if (board[i][j] == 0) | |
{ | |
printf("\t_"); | |
} | |
} | |
printf("\t|||\n"); | |
} | |
printf("\n"); | |
} | |
/** | |
* If tile borders empty space, moves tile and returns true, else | |
* returns false. | |
*/ | |
bool move(int tile) | |
{ | |
int tempholder = tile; | |
int tempholderfor0; | |
for (int i = 0; i < d; i++) | |
{ | |
for (int j = 0; j < d; j++) | |
{ | |
if(tile == board[i][j]) | |
{ | |
if ((board[i][j-1] == 0) && (board[i][j+1] !=0) && (board[i+1][j] !=0) && (board[i-1][j] !=0) ) | |
{ | |
tempholderfor0 = board[i][j-1]; | |
{board[i][j] = tempholderfor0;} | |
board[i][j-1] = tempholder; | |
return true; | |
} | |
else if ((board[i][j-1] != 0) && (board[i][j+1] ==0) && (board[i+1][j] !=0) && (board[i-1][j] !=0) ) | |
{ | |
tempholderfor0 = board[i][j+1]; | |
{board[i][j] = tempholderfor0;} | |
board[i][j+1] = tempholder; | |
return true; | |
} | |
else if ((board[i][j-1] != 0) && (board[i][j+1] !=0) && (board[i+1][j] !=0) && (board[i-1][j] ==0) ) | |
{ | |
tempholderfor0 = board[i-1][j]; | |
{board[i][j] = tempholderfor0;} | |
board[i-1][j] = tempholder; | |
return true; | |
} | |
else if ((board[i][j-1] != 0) && (board[i][j+1] !=0) && (board[i+1][j] ==0) && (board[i-1][j] !=0) ) | |
{ | |
tempholderfor0 = board[i+1][j]; | |
{board[i][j] = tempholderfor0;} | |
board[i+1][j] = tempholder; | |
return true; | |
} | |
else if ((board[i][j] == board[0][0]) && (board[i][j] == tile)) | |
{ | |
if (board[i+1][j] == 0) | |
{ | |
tempholderfor0 = board[i+1][j]; | |
{board[i][j] = tempholderfor0;} | |
board[i+1][j] = tempholder; | |
return true; | |
} | |
else if (board[i][j+1] == 0) | |
{ | |
tempholderfor0 = board[i][j+1]; | |
{board[i][j] = tempholderfor0;} | |
board[i][j+1] = tempholder; | |
return true; | |
} | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
} | |
} | |
return false; | |
} | |
/** | |
* Returns true if game is won (i.e., board is in winning configuration), | |
* else false. | |
*/ | |
bool won(void) | |
{ | |
n = 0; | |
for (int i = 0; i < d; i++) | |
{ | |
for (int j = 1; j < d; j++) | |
{ | |
n = n + 1; | |
if (board[i][j] != n) | |
{ | |
return false; | |
} | |
} | |
} | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* find.c | |
* | |
* Computer Science 50 | |
* Problem Set 3 | |
* | |
* Prompts user for as many as MAX values until EOF is reached, | |
* then proceeds to search that "haystack" of values for given needle. | |
* | |
* Usage: ./find needle | |
* | |
* where needle is the value to find in a haystack of values | |
*/ | |
#include <cs50.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include "helpers.h" | |
// maximum amount of hay | |
const int MAX = 65536; | |
int main(int argc, string argv[]) | |
{ | |
// ensure proper usage | |
if (argc != 2) | |
{ | |
printf("Usage: ./find needle\n"); | |
return -1; | |
} | |
// remember needle | |
int needle = atoi(argv[1]); | |
// fill haystack | |
int size; | |
int haystack[MAX]; | |
for (size = 0; size < MAX; size++) | |
{ | |
// wait for hay until EOF | |
printf("\nhaystack[%i] = ", size); | |
int straw = GetInt(); | |
if (straw == INT_MAX) | |
{ | |
break; | |
} | |
// add hay to stack | |
haystack[size] = straw; | |
} | |
printf("\n"); | |
// sort the haystack | |
sort(haystack, size); | |
// try to find needle in haystack | |
if ( (search(needle, haystack, size)) == true) | |
{ | |
printf("\nFound needle in haystack!\n\n"); | |
return 0; | |
} | |
else | |
{ | |
printf("\nDidn't find needle in haystack.\n\n"); | |
return 1; | |
} | |
} |
Author
tylersloeper
commented
Dec 2, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment