added everything i had in this folder
This commit is contained in:
commit
b727aef8b5
80
battleships.c
Normal file
80
battleships.c
Normal file
|
@ -0,0 +1,80 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
enum {
|
||||
N_SHIPS,
|
||||
WIDTH = 8,
|
||||
HEIGHT = 8,
|
||||
};
|
||||
|
||||
struct position {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
struct ship {
|
||||
int length;
|
||||
struct position position;
|
||||
char sunken;
|
||||
char alignment;
|
||||
char *hits;
|
||||
};
|
||||
|
||||
struct game_state {
|
||||
char running;
|
||||
char board[WIDTH][HEIGHT];
|
||||
struct ship ships[N_SHIPS];
|
||||
};
|
||||
|
||||
struct ship create_ship(int length, struct position position);
|
||||
void delete_ship(struct ship ship);
|
||||
|
||||
void hit(struct game_state *game_state, struct position position);
|
||||
|
||||
struct game_state init_game_state();
|
||||
void deinit_game_state(struct game_state *game_state);
|
||||
char check_win_condition(struct game_state *game_state);
|
||||
|
||||
struct position read_position();
|
||||
|
||||
int main(void) {
|
||||
struct game_state game_state = init_game_state();
|
||||
while (game_state.running) {
|
||||
struct position hit_position = read_position();
|
||||
hit(&game_state, hit_position);
|
||||
if (check_win_condition(&game_state)) {
|
||||
game_state.running = 0;
|
||||
printf("You win!");
|
||||
}
|
||||
}
|
||||
deinit_game_state(&game_state);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct ship create_ship(int length, struct position position) {
|
||||
struct ship ship = {.length = length, .position = position, .sunken = 0};
|
||||
ship.hits = calloc(length, sizeof *ship.hits);
|
||||
return ship;
|
||||
}
|
||||
void delete_ship(struct ship ship) {
|
||||
free(ship.hits);
|
||||
}
|
||||
|
||||
void hit(struct game_state *game_state, struct position position);
|
||||
|
||||
struct game_state init_game_state() {
|
||||
struct game_state game_state;
|
||||
for (int i = 0; i < N_SHIPS; ++i) {
|
||||
/* generate ship */
|
||||
}
|
||||
}
|
||||
|
||||
void deinit_game_state(struct game_state *game_state) {
|
||||
for (int i = 0; i < N_SHIPS; ++i) {
|
||||
delete_ship(game_state->ships[i]);
|
||||
}
|
||||
}
|
||||
|
||||
char check_win_condition(struct game_state *game_state);
|
||||
|
||||
struct position read_position();
|
156
calculator.c
Normal file
156
calculator.c
Normal file
|
@ -0,0 +1,156 @@
|
|||
#include <stdio.h>
|
||||
|
||||
/**
|
||||
* calculate the sum of two numbers
|
||||
* @param number1 first summand
|
||||
* @param number2 second summand
|
||||
* @return sum of the two summands
|
||||
*/
|
||||
int add(int number1, int number2);
|
||||
|
||||
/**
|
||||
* calculate the product of two numbers
|
||||
* @param number1 first factor
|
||||
* @param number2 second factor
|
||||
* @return product of the two factors
|
||||
*/
|
||||
float multiply(int number1, float number2);
|
||||
|
||||
/**
|
||||
* calculate the power of two integers
|
||||
* @param number1 basis
|
||||
* @param number2 exponent
|
||||
* @returns number1 to the power of number2
|
||||
*/
|
||||
int power(int number1, int number2);
|
||||
|
||||
/**
|
||||
* calculate the remainder of the division of two numbers
|
||||
* @param number1 divident
|
||||
* @param number2 divisor
|
||||
* @returns number1 modulo number2
|
||||
*/
|
||||
int modulo(int number1, int number2);
|
||||
|
||||
/**
|
||||
* calculate the factorial
|
||||
* @param number number to calculate factorial of
|
||||
* @return number!
|
||||
*/
|
||||
int factorial(int number);
|
||||
|
||||
/**
|
||||
* calculate a fibonacci number
|
||||
* @param number what fibonacci number should be computed?
|
||||
* @returns numberth fibonacci number
|
||||
*/
|
||||
int fib(int number);
|
||||
|
||||
int main(void) {
|
||||
int number1;
|
||||
int number2;
|
||||
char operator;
|
||||
|
||||
printf("give me two numbers and an operator seperated by spaces\n");
|
||||
int n_matched = scanf("%d %c %d", &number1, &operator, &number2);
|
||||
|
||||
if (n_matched != 3) {
|
||||
printf("Failure!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
switch (operator) {
|
||||
case '+':
|
||||
case 'a':
|
||||
case 'A':
|
||||
printf("%d\n", add(number1, number2));
|
||||
break;
|
||||
case '-':
|
||||
case 's':
|
||||
case 'S':
|
||||
printf("%d\n", add(number1, -number2));
|
||||
break;
|
||||
case '*':
|
||||
case 'm':
|
||||
case 'M':
|
||||
printf("%d\n", multiply(number1, number2));
|
||||
break;
|
||||
case '/':
|
||||
case 'd':
|
||||
case 'D':
|
||||
printf("%f\n", multiply(number1, 1.0 / number2));
|
||||
break;
|
||||
case '%':
|
||||
case 'r':
|
||||
case 'R':
|
||||
printf("%d\n", modulo(number1, number2));
|
||||
break;
|
||||
default:
|
||||
printf("are you stupid? thats not an operator!!!1\n");
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int add(int number1, int number2) {
|
||||
return number1 + number2;
|
||||
}
|
||||
|
||||
float multiply(int number1, float number2) {
|
||||
/* recursive approach */
|
||||
if (number1 == 0) {
|
||||
return 0;
|
||||
}
|
||||
return number2 + multiply(number1-1, number2);
|
||||
|
||||
/* looping approach */
|
||||
float multiple = 0;
|
||||
for (int i = 0; i < number1; ++i) {
|
||||
multiple += number2;
|
||||
}
|
||||
return multiple;
|
||||
|
||||
/* trivial approach */
|
||||
return number1 * number2;
|
||||
}
|
||||
|
||||
int power(int number1, int number2) {
|
||||
/* looping approach */
|
||||
int power_value = 1;
|
||||
for (int i = 0; i < number2; ++i) {
|
||||
power_value *= number1;
|
||||
}
|
||||
return power_value;
|
||||
|
||||
/* recursive approach */
|
||||
if (number2 == 0) {
|
||||
return 1;
|
||||
}
|
||||
return multiply(number1,power(number1, number2 - 1));
|
||||
}
|
||||
|
||||
int modulo(int number1, int number2) {
|
||||
return number1 % number2;
|
||||
}
|
||||
|
||||
int factorial(unsigned number) {
|
||||
if (number == 0) {
|
||||
return 1;
|
||||
}
|
||||
return number * factorial(number - 1);
|
||||
}
|
||||
|
||||
int fib(unsigned number) {
|
||||
if (number == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (number == 1) {
|
||||
return 0;
|
||||
}
|
||||
if (number == 2) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return fib(number - 1) + fib(number - 2);
|
||||
}
|
43
circle.c
Normal file
43
circle.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
struct point {
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
|
||||
struct circle {
|
||||
struct point center;
|
||||
float radius;
|
||||
float circumference;
|
||||
float area;
|
||||
};
|
||||
|
||||
struct circle read_circle();
|
||||
|
||||
struct circle complete_circle(struct circle circle);
|
||||
|
||||
int main(void) {
|
||||
struct circle circle;
|
||||
circle = read_circle();
|
||||
circle = complete_circle(circle);
|
||||
|
||||
printf("Circumference: %f\n", circle.circumference);
|
||||
printf("Area: %f\n", circle.area);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct circle read_circle() {
|
||||
struct circle circle;
|
||||
printf("gimme da circ (<x,y> r): ");
|
||||
char buf[256] = {0};
|
||||
fgets(buf, sizeof(buf) / sizeof(buf[0]), stdin);
|
||||
sscanf(buf, "<%f,%f> %f", &circle.center.x, &circle.center.y, &circle.radius);
|
||||
return circle;
|
||||
}
|
||||
|
||||
struct circle complete_circle(struct circle circle) {
|
||||
circle.circumference = M_PI * 2 * circle.radius;
|
||||
circle.area = M_PI * circle.radius * circle.radius;
|
||||
return circle;
|
||||
}
|
45
complex.c
Normal file
45
complex.c
Normal file
|
@ -0,0 +1,45 @@
|
|||
#include <stdio.h>
|
||||
|
||||
/**
|
||||
* A Complex number consisting of real and imaginary part
|
||||
*/
|
||||
struct complex_number {
|
||||
int real; /**< real part of complex number */
|
||||
int imag; /**< imaginary part of complex number */
|
||||
};
|
||||
|
||||
/**
|
||||
* read a complex number from command line
|
||||
*
|
||||
* @returns the compex number from command line
|
||||
*/
|
||||
struct complex_number read_complex_number();
|
||||
|
||||
/**
|
||||
* print a complex number to command line
|
||||
*
|
||||
* @param number the complex number to print
|
||||
*/
|
||||
void print_complex_number(struct complex_number number);
|
||||
|
||||
int main(void) {
|
||||
struct complex_number number;
|
||||
number = read_complex_number();
|
||||
print_complex_number(number);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct complex_number read_complex_number() {
|
||||
struct complex_number number;
|
||||
printf("please input a complex number in form a+bi: ");
|
||||
char buf[256] = {0};
|
||||
fgets(buf, sizeof(buf) / sizeof(buf[0]), stdin);
|
||||
sscanf(buf, "%d+%di", &number.real, &number.imag);
|
||||
return number;
|
||||
}
|
||||
|
||||
void print_complex_number(struct complex_number number) {
|
||||
printf("%d+%di\n", number.real, number.imag);
|
||||
}
|
||||
|
30
fib.c
Normal file
30
fib.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
int fib;
|
||||
printf("fib? fib! fib? ");
|
||||
scanf("%d", &fib);
|
||||
|
||||
int current = 0;
|
||||
int next = 1;
|
||||
|
||||
if (fib < 1) {
|
||||
printf("Wait, wat?\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (fib == 1) {
|
||||
printf("fib: %d\n", current);
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (int i = 2; i < fib; ++i) {
|
||||
int new = current + next;
|
||||
current = next;
|
||||
next = new;
|
||||
}
|
||||
|
||||
printf("fib: %d\n", next);
|
||||
|
||||
return 0;
|
||||
}
|
102
hangman.c
Normal file
102
hangman.c
Normal file
|
@ -0,0 +1,102 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
char *gallows[] = {
|
||||
"\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"=============\n",
|
||||
"\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"\n"
|
||||
"\n"
|
||||
" -----\n"
|
||||
" / \\\n"
|
||||
" / \\\n"
|
||||
"=============\n",
|
||||
"\n"
|
||||
" |\n"
|
||||
" |\n"
|
||||
" |\n"
|
||||
" |\n"
|
||||
" -----\n"
|
||||
" / \\\n"
|
||||
" / \\\n"
|
||||
"=============\n",
|
||||
" -----\n"
|
||||
" |/\n"
|
||||
" |\n"
|
||||
" |\n"
|
||||
" |\n"
|
||||
" -----\n"
|
||||
" / \\\n"
|
||||
" / \\\n"
|
||||
"=============\n",
|
||||
" -----\n"
|
||||
" |/ o\n"
|
||||
" | ^W^\n"
|
||||
" | / \\\n"
|
||||
" |\n"
|
||||
" -----\n"
|
||||
" / \\\n"
|
||||
" / \\\n"
|
||||
"=============\n",
|
||||
};
|
||||
|
||||
void print_state(int state, char *placeholder, char *wrong_guesses);
|
||||
int check_input(char *word, char *placeholder, char input);
|
||||
|
||||
int main(void) {
|
||||
int state = 0;
|
||||
char word[] = "gameover";
|
||||
char placeholder[] = "________";
|
||||
char wrong_guesses[6] = {0};
|
||||
|
||||
while (state < 5) {
|
||||
print_state(state, placeholder, wrong_guesses);
|
||||
char input;
|
||||
do {
|
||||
input = getchar();
|
||||
} while (input == '\n');
|
||||
|
||||
if (check_input(word, placeholder, input)) {
|
||||
if (strcmp(word, placeholder) == 0) {
|
||||
printf("You won!");
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
wrong_guesses[state] = input;
|
||||
state++;
|
||||
if (state == 5) {
|
||||
printf("You lost!");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void print_state(int state, char *placeholder, char *wrong_guesses) {
|
||||
printf(gallows[state]);
|
||||
printf("word: %s\n", placeholder);
|
||||
printf("wrong: %s\n", wrong_guesses);
|
||||
}
|
||||
|
||||
int check_input(char *word, char *placeholder, char input) {
|
||||
int occurences = 0;
|
||||
while (*word != '\0') {
|
||||
if (*word == input) {
|
||||
*placeholder = input;
|
||||
occurences++;
|
||||
}
|
||||
word++;
|
||||
placeholder++;
|
||||
}
|
||||
return occurences;
|
||||
}
|
25
main.c
Normal file
25
main.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
char input_character1;
|
||||
char input_character2;
|
||||
char input_character3;
|
||||
char input_character4;
|
||||
char input_character5;
|
||||
|
||||
printf("Char 1");
|
||||
scanf("%c", &input_character1);
|
||||
printf("Char 2");
|
||||
scanf("\n%c", &input_character2);
|
||||
printf("Char 3");
|
||||
scanf("\n%c", &input_character3);
|
||||
printf("Char 4");
|
||||
scanf("\n%c", &input_character4);
|
||||
printf("Char 5");
|
||||
scanf("\n%c", &input_character5);
|
||||
|
||||
printf("%d %d %d %d %d\n", input_character1, input_character2, input_character3, input_character4, input_character5);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
8
print_ptr.c
Normal file
8
print_ptr.c
Normal file
|
@ -0,0 +1,8 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
int a = 42;
|
||||
int *pa = &a;
|
||||
printf("%p\n", pa);
|
||||
return 0;
|
||||
}
|
38
printing_strings.c
Normal file
38
printing_strings.c
Normal file
|
@ -0,0 +1,38 @@
|
|||
#include <stdio.h>
|
||||
|
||||
/**
|
||||
* print a given string char by char using pointer arithmetic
|
||||
* @param str string to print
|
||||
*/
|
||||
void print_string(char *str);
|
||||
|
||||
/**
|
||||
* similar to print_string but using recursion
|
||||
* @param str string to print
|
||||
*/
|
||||
void print_string_rec(char *str);
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
print_string(*argv);
|
||||
argv++;
|
||||
putchar('\n');
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void print_string(char *str) {
|
||||
while(*str != '\0') {
|
||||
putchar(*str);
|
||||
str++;
|
||||
}
|
||||
}
|
||||
|
||||
void print_string_rec(char *str) {
|
||||
if (*str == '\0') {
|
||||
return;
|
||||
}
|
||||
putchar(*str);
|
||||
++str;
|
||||
print_string_rec(str);
|
||||
}
|
31
sheldon.c
Normal file
31
sheldon.c
Normal file
|
@ -0,0 +1,31 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
int n_knocks;
|
||||
printf("how many knocks?\n");
|
||||
scanf("%d", &n_knocks);
|
||||
|
||||
for (int i = 0; i < n_knocks; ++i) {
|
||||
printf("Knock, knock, knock - Penny?\n");
|
||||
}
|
||||
|
||||
int n_actual_knocks;
|
||||
printf("and how many actual knocks?\n");
|
||||
scanf("%d", &n_actual_knocks);
|
||||
|
||||
for (int i = 0; i < n_knocks; ++i) {
|
||||
for (int j = 0; j < n_actual_knocks; ++j) {
|
||||
if (j == 0) {
|
||||
printf("Knock");
|
||||
} else {
|
||||
printf(", knock");
|
||||
}
|
||||
}
|
||||
if (n_actual_knocks != 0) {
|
||||
printf(" - ");
|
||||
}
|
||||
printf("Penny.\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
171
tetris.c
Normal file
171
tetris.c
Normal file
|
@ -0,0 +1,171 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
enum {
|
||||
HEIGHT = 20,
|
||||
WIDTH = 10,
|
||||
};
|
||||
|
||||
struct position {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
struct rotation {
|
||||
struct position tiles[4];
|
||||
};
|
||||
|
||||
struct brick {
|
||||
struct rotation *states;
|
||||
int state;
|
||||
struct position position;
|
||||
};
|
||||
|
||||
/************************
|
||||
* Templates for bricks *
|
||||
************************/
|
||||
|
||||
/* T Brick */
|
||||
struct rotation t_brick_rotations[4] = {
|
||||
{ /* first rotation */
|
||||
.tiles = {
|
||||
{.x = 2, .y = 0},
|
||||
{.x = 1, .y = 1},
|
||||
{.x = 2, .y = 1},
|
||||
{.x = 2, .y = 2}
|
||||
}
|
||||
},
|
||||
{ /* second rotation */
|
||||
.tiles = {
|
||||
{.x = 2, .y = 0},
|
||||
{.x = 1, .y = 1},
|
||||
{.x = 2, .y = 1},
|
||||
{.x = 3, .y = 1}
|
||||
}
|
||||
},
|
||||
{ /* third rotation */
|
||||
.tiles = {
|
||||
{.x = 2, .y = 0},
|
||||
{.x = 2, .y = 1},
|
||||
{.x = 3, .y = 1},
|
||||
{.x = 2, .y = 2}
|
||||
}
|
||||
},
|
||||
{ /* fourth rotation */
|
||||
.tiles = {
|
||||
{.x = 1, .y = 1},
|
||||
{.x = 2, .y = 1},
|
||||
{.x = 3, .y = 1},
|
||||
{.x = 2, .y = 2}
|
||||
}
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
struct game_state {
|
||||
char running;
|
||||
char game_grid[WIDTH][HEIGHT];
|
||||
struct brick current_brick;
|
||||
};
|
||||
|
||||
enum input {
|
||||
I_QUIT,
|
||||
I_LEFT,
|
||||
I_RIGHT,
|
||||
I_DROP,
|
||||
I_ROTATE,
|
||||
};
|
||||
|
||||
struct game_state construct_game_state();
|
||||
|
||||
struct brick spawn_brick();
|
||||
|
||||
/* gravitate brick (darf der das?) */
|
||||
|
||||
/* (optional) drop brick */
|
||||
|
||||
/* should brick fall? */
|
||||
|
||||
/* rotate brick */
|
||||
|
||||
/* check for full line */
|
||||
|
||||
/* remove full line */
|
||||
|
||||
/* move brick */
|
||||
|
||||
/* get input */
|
||||
|
||||
/* evaluate input */
|
||||
|
||||
/* brick to grid */
|
||||
|
||||
/* loose condition */
|
||||
|
||||
void draw_game_state(struct game_state game_state);
|
||||
|
||||
int brick_on_position(int x, int y, struct brick brick);
|
||||
|
||||
int main(void) {
|
||||
struct game_state game_state = construct_game_state();
|
||||
while (game_state.running) {
|
||||
draw_game_state(game_state);
|
||||
//get input
|
||||
//evaluate input
|
||||
//game logic
|
||||
game_state.current_brick.position.y++;
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
struct game_state construct_game_state() {
|
||||
struct game_state game_state = {
|
||||
.running = 1,
|
||||
.game_grid = {0},
|
||||
.current_brick = spawn_brick()
|
||||
};
|
||||
return game_state;
|
||||
}
|
||||
|
||||
struct brick spawn_brick() {
|
||||
struct brick brick = {
|
||||
.states = t_brick_rotations,
|
||||
.state = 0,
|
||||
.position = {.x = 3, .y=0}
|
||||
};
|
||||
return brick;
|
||||
}
|
||||
|
||||
void draw_game_state(struct game_state game_state) {
|
||||
system("clear");
|
||||
for (int y = 0; y < HEIGHT; ++y) {
|
||||
printf("|");
|
||||
for (int x = 0; x < WIDTH; ++x) {
|
||||
if (game_state.game_grid[x][y]
|
||||
|| brick_on_position(x, y, game_state.current_brick)) {
|
||||
printf("O");
|
||||
} else {
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
printf("|\n");
|
||||
}
|
||||
printf("|");
|
||||
for (int x = 0; x < WIDTH; ++x) {
|
||||
printf("-");
|
||||
}
|
||||
printf("|\n");
|
||||
}
|
||||
|
||||
int brick_on_position(int x, int y, struct brick brick) {
|
||||
x -= brick.position.x;
|
||||
y -= brick.position.y;
|
||||
struct rotation *rotation = &brick.states[brick.state];
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
if (rotation->tiles[i].x == x && rotation->tiles[i].y == y) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
8
unassigned.c
Normal file
8
unassigned.c
Normal file
|
@ -0,0 +1,8 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
char x = 64;
|
||||
|
||||
printf("%c\n", x);
|
||||
printf("%d\n", x);
|
||||
}
|
Loading…
Reference in a new issue