#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#define MIN 4
int leia_rows;
int leia_col;
int difficulty_troopers(int rows, int col, int diff);
int difficulty_obstacles(int rows, int col, int diff);
void start_table(char **p,int rows,int col);
void random_stuff(char **p, int rows, int col, int diff);
int play(char **p, char **v, int rows, int col);
void ask_help(char **p, char **v,int rows, int col);
void change_obstacle(char **p, char **v, int rows, int col);
void move(char **p, char **v, int rows, int col, int vert, int horiz);
void leia_visibility(char **p,int rows,int col);
int main(){
int diff;
int rows,col,i,j;
char **arr = NULL;
char **arr1 = NULL;
leia_row = -1;
leia_col = -1;
printf("\nWelcome to Princess's Leia spaceship!\n");
printf("Can you find R2D2, while being chased by the Republic ?\n\n");
do{
printf("Pick rows size: ");
scanf("%d",&rows);
printf("Pick columns size: ");
scanf("%d",&col);
}while(rows <1 || col < 1);
do{
printf("Pick number for difficulty:\n");
printf("1.Easy\n2.Medium\n3.Hard\n4.Impossible\n");
scanf("%d",&diff);
}while(diff < 1 || diff > 4);
//while(rows*col > MIN){
arr = (char**)malloc(rows * sizeof(char*));
if(arr == NULL){
printf("FAILED MEMORY ALLOCATION 1\n");
return -1;
}
else{
for(i = 0; i < rows; i++){
arr[i] = (char*)malloc(col * (sizeof(char)));
if(arr[i] == NULL){
printf("FAILED MEMORY ALLOCATION 2\n");
return -1;
}
}
}
/* for(i = 0; i < rows; i++){
for(j = 0; j < col; j++){
arr[i][j] = '#';
}
} */
// }
srand(time(NULL));
leia_rows = rand() % rows;
leia_col = rand() % col;
p[leia_rows][leia_col] = 'L';
switch(diff){
case 1:
random_stuff(arr, rows, col, diff);
// play(arr, arr1, rows,col);
break;
case 2:
random_stuff(arr, rows, col, diff);
//play(arr, arr1, rows,col);
break;
case 3:
random_stuff(arr, rows, col, diff);
//play(arr, arr1, rows,col);
break;
case 4:
random_stuff(arr, rows, col, diff);
//play(arr, arr1, rows,col);
break;
}
return 0;
}
int difficulty_troopers(int rows, int col, int diff){
if(diff == 1){
return round(rows * col * (0.02)); // stroggylopoiei
}
else if(diff == 2){
return round(rows * col * (0.05));
}
else if(diff == 3){
return round(rows * col * (0.1));
}
else if(diff == 4){
return round(rows * col * (0.15));
}
else{
return 0;
}
}
int difficulty_obstacles(int rows, int col, int diff){
if(diff == 1){
return round(rows * col * (0.13));
}
else if(diff == 2){
return round(rows * col * (0.1));
}
else if(diff == 3){
return round(rows * col * (0.05));
}
else if(diff == 4){
return 0;
}
else{
return 0;
}
}
void start_table(char **p, int rows, int col){ // EKTIPOSI
int i,j;
int x,y;
char c = 'A';
printf(" %c",c);
for(j = 0; j < col - 1; j++){
printf(" %c", c + 1);
c+=1;
}
printf("\n");
printf("---");
for(j = 0; j < col - 1; j++){
printf("-----");
}
printf("---\n");
for(i = 0; i < rows; i++){
printf(" %d |",i + 1);
for(j = 0; j < col; j++){
if(p[i][j] == 'L' || p[i][j] == 'V' || p[i][j] == 'X'){
printf(" %c ", p[i][j]);
}
else{
printf(" %c ", '#');
}
printf("\n");
}
}
void random_stuff(char **p, int rows, int col, int diff){
int i,j;
int o = 0;
int t = 0;
int characters = 0;
srand(time(NULL));
// TOPOTHETISI OBSTACLES KAI TROOPERS
while(t < difficulty_troopers(rows, col, diff) ){
i = rand() % rows;
j = rand() % col;
if((i >= 0) && (j >= 0) && (i < rows) && (j < col)){
if((i != leia_rows || j != leia_col) && p[i][j] != 'X' && p[i][j] != '@'){
p[i][j] = '@';
t++;
}
}
else{
t--;
}
}
while(o < difficulty_obstacles(rows, col, diff)){
i = rand() % rows;
j = rand() % col;
if((i >= 0) && (j >= 0) && (i < rows) && (j < col)){
if((i != leia_rows || j != leia_col) && p[i][j] != '@' && p[i][j] != 'X'){
p[i][j] = 'X';
o++;
}
}
else{
o--;
}
}
i = rand() % rows;
j = rand() % col;
if(p[i][j] != '@' && p[i][j] != 'X' && p[i][j] != 'L'){
p[i][j] = 'V';
}
i = rand() % rows;
j = rand() % col;
if(p[i][j] != '@' && p[i][j] != 'X' && p[i][j] != 'L' && p[i][j] != 'V'){
p[i][j] = 'R';
}
for(i = 0; i < rows; i++){
p[i][leia_col] = '.';
}
for(j = 0; j < col; j++){}
p[leia_rows][j] = '.';
}
p[leia_rows][leia_col] = 'L';
start_table(p, rows, col);
}
/* int play(char **p,char **v, int rows, int col){
char move[10];
char choice[10];
int i;
int a = 0;
int x = 0, y = 0;
printf("\nMake your move(s): ");
scanf("%s",choice);
for(int i = 0; i < strlen(choice); i++){
choice[i] = tolower(choice[i]);
}
while(a != 1){
if(strcmp(choice, "x") == 0){
return 1;
a = 1; // kanei exit...as poume
}
else if(strcmp(choice, "h") == 0){
ask_help(p, v, rows, col); // ftiaksimo sto kalesma
}
else{
for(int i = 0; i < strlen(choice); i++){
if(i == 2){
if(choice[2] == '>'){
change_obstacle(p, v, rows, col);
}
}
if(choice[i] == 'u'){
move(p, v, rows, col, x - 1, y);
}
else if(choice[i] == 'd'){
move(p, v, rows, col, x + 1, y);
}
else if(choice[i] == 'l'){
move(p, v, rows, col, x, y - 1);
}
else if(choice[i] == 'r'){
move(p, v, rows, col, x, y + 1);
}
}
}
}
} */
unsigned long golay(unsigned long cw)
/* This function calculates [23,12] Golay codewords.
The format of the returned longint is
[checkbits(11),data(12)]. */
{
int i;
unsigned long c;
cw&=0xfffl;
c=cw; /* save original codeword */
for (i=1; i<=12; i++) /* examine each data bit */
{
if (cw & 1) /* test data bit */
cw^=POLY; /* XOR polynomial */
cw>>=1; /* shift intermediate result */
}
return((cw<<12)|c); /* assemble codeword */
}
/* ====================================================== */
int parity(unsigned long cw)
/* This function checks the overall parity of codeword cw.
If parity is even, 0 is returned, else 1. */
{
unsigned char p;
/* XOR the bytes of the codeword */
p=*(unsigned char*)&cw;
p^=*((unsigned char*)&cw+1);
p^=*((unsigned char*)&cw+2);
/* XOR the halves of the intermediate result */
p=p ^ (p>>4);
p=p ^ (p>>2);
p=p ^ (p>>1);
/* return the parity result */
return(p & 1);
}
/* ====================================================== */
unsigned long syndrome(unsigned long cw)
/* This function calculates and returns the syndrome
of a [23,12] Golay codeword. */
{
int i;
cw&=0x7fffffl;
for (i=1; i<=12; i++) /* examine each data bit */
{
if (cw & 1) /* test data bit */
cw^=POLY; /* XOR polynomial */
cw>>=1; /* shift intermediate result */
}
return(cw<<12); /* value pairs with upper bits of cw */
}
/* ====================================================== */
int weight(unsigned long cw)
/* This function calculates the weight of
23 bit codeword cw. */
{
int bits,k;
/* nibble weight table */
const char wgt[16] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
bits=0; /* bit counter */
k=0;
/* do all bits, six nibbles max */
while ((k<6) && (cw))
{
bits=bits+wgt[cw & 0xf];
cw>>=4;
k++;
}
return(bits);
}
/* ====================================================== */
unsigned long rotate_left(unsigned long cw, int n)
/* This function rotates 23 bit codeword cw left by n bits. */
{
int i;
if (n != 0)
{
for (i=1; i<=n; i++)
{
if ((cw & 0x400000l) != 0)
cw=(cw << 1) | 1;
else
cw<<=1;
}
}
return(cw & 0x7fffffl);
}
/* ====================================================== */
unsigned long rotate_right(unsigned long cw, int n)
/* This function rotates 23 bit codeword cw right by n bits. */
{
int i;
if (n != 0)
{
for (i=1; i<=n; i++)
{
if ((cw & 1) != 0)
cw=(cw >> 1) | 0x400000l;
else
cw>>=1;
}
}
return(cw & 0x7fffffl);
}
/* ====================================================== */
unsigned long correct(unsigned long cw, int *errs)
/* This function corrects Golay [23,12] codeword cw, returning the
corrected codeword. This function will produce the corrected codeword
for three or fewer errors. It will produce some other valid Golay
codeword for four or more errors, possibly not the intended
one. *errs is set to the number of bit errors corrected. */
{
unsigned char
w; /* current syndrome limit weight, 2 or 3 */
unsigned long
mask; /* mask for bit flipping */
int
i,j; /* index */
unsigned long
s, /* calculated syndrome */
cwsaver; /* saves initial value of cw */
cwsaver=cw; /* save */
*errs=0;
w=3; /* initial syndrome weight threshold */
j=-1; /* -1 = no trial bit flipping on first pass */
mask=1;
while (j<23) /* flip each trial bit */
{
if (j != -1) /* toggle a trial bit */
{
if (j>0) /* restore last trial bit */
{
cw=cwsaver ^ mask;
mask+=mask; /* point to next bit */
}
cw=cwsaver ^ mask; /* flip next trial bit */
w=2; /* lower the threshold while bit diddling */
}
s=syndrome(cw); /* look for errors */
if (s) /* errors exist */
{
for (i=0; i<23; i++) /* check syndrome of each cyclic shift */
{
if ((*errs=weight(s)) <= w) /* syndrome matches error pattern */
{
cw=cw ^ s; /* remove errors */
cw=rotate_right(cw,i); /* unrotate data */
return(s=cw);
}
else
{
cw=rotate_left(cw,1); /* rotate to next pattern */
s=syndrome(cw); /* calc new syndrome */
}
}
j++; /* toggle next trial bit */
}
else
return(cw); /* return corrected codeword */
}
return(cwsaver); /* return original if no corrections */
} /* correct */
/* ====================================================== */
int decode(int correct_mode, int *errs, unsigned long *cw)
/* This function decodes codeword *cw in one of two modes. If correct_mode
is nonzero, error correction is attempted, with *errs set to the number of
bits corrected, and returning 0 if no errors exist, or 1 if parity errors
exist. If correct_mode is zero, error detection is performed on *cw,
returning 0 if no errors exist, 1 if an overall parity error exists, and
2 if a codeword error exists. */
{
unsigned long parity_bit;
if (correct_mode) /* correct errors */
{
parity_bit=*cw & 0x800000l; /* save parity bit */
*cw&=~0x800000l; /* remove parity bit for correction */
*cw=correct(*cw, errs); /* correct up to three bits */
*cw|=parity_bit; /* restore parity bit */
/* check for 4 bit errors */
if (parity(*cw)) /* odd parity is an error */
return(1);
return(0); /* no errors */
}
else /* detect errors only */
{
*errs=0;
if (parity(*cw)) /* odd parity is an error */
{
*errs=1;
return(1);
}
if (syndrome(*cw))
{
*errs=1;
return(2);
}
else
return(0); /* no errors */
}
} /* decode */
/* ====================================================== */
void golay_test(void)
/* This function tests the Golay routines for detection and correction
of various patterns of error_limit bit errors. The error_mask cycles
over all possible values, and error_limit selects the maximum number
of induced errors. */
{
unsigned long
error_mask, /* bitwise mask for inducing errors */
trashed_codeword, /* the codeword for trial correction */
virgin_codeword; /* the original codeword without errors */
unsigned char
pass=1, /* assume test passes */
error_limit=3; /* select number of induced bit errors here */
int
error_count; /* receives number of errors corrected */
virgin_codeword=golay(0x555); /* make a test codeword */
if (parity(virgin_codeword))
virgin_codeword^=0x800000l;
for (error_mask=0; error_mask<0x800000l; error_mask++)
{
/* filter the mask for the selected number of bit errors */
if (weight(error_mask) <= error_limit) /* you can make this faster! */
{
trashed_codeword=virgin_codeword ^ error_mask; /* induce bit errors */
decode(1,&error_count,&trashed_codeword); /* try to correct bit errors */
if (trashed_codeword ^ virgin_codeword)
{
printf("Unable to correct %d errors induced with error mask = 0x%lX\n",
weight(error_mask),error_mask);
pass=0;
}
if (kbhit()) /* look for user input */
{
if (getch() == 27) return; /* escape exits */
/* other key prints status */
printf("Current test count = %ld of %ld\n",error_mask,0x800000l);
}
}
}
printf("Golay test %s!\n",pass?"PASSED":"FAILED");
}
/* ====================================================== */
void main(int argument_count, char *argument[])
{
int i,j;
unsigned long l,g;
const char *errmsg = "Usage: G DATA Encode/Correct/Verify/Test\n\n"
" where DATA is the data to be encoded, codeword to be corrected,\n"
" or codeword to be checked for errors. DATA is hexadecimal.\n\n"
"Examples:\n\n"
" G 555 E encodes information value 555 and prints a codeword\n"
" G ABC123 C corrects codeword ABC123\n"
" G ABC123 V checks codeword ABC123 for errors\n"
" G ABC123 T tests routines, ABC123 is a dummy parameter\n\n";
if (argument_count != 3)
{
printf(errmsg);
exit(0);
}
if (sscanf(argument[1],"%lx",&l) != 1)
{
printf(errmsg);
exit(0);
}
switch (toupper(*argument[2]))
{
case 'E': /* encode */
l&=0xfff;
l=golay(l);
if (parity(l)) l^=0x800000l;
printf("Codeword = %lX\n",l);
break;
case 'V': /* verify */
if (decode(0,&i,&l))
printf("Codeword %lX is not a Golay codeword.\n",l);
else
printf("Codeword %lX is a Golay codeword.\n",l);
break;
case 'C': /* correct */
g=l; /* save initial codeword */
j=decode(1,&i,&l);
if ((j) && (i))
printf("Codeword %lX had %d bits corrected,\n"
"resulting in codeword %lX with a parity error.\n",g,i,l);
else
if ((j == 0) && (i))
printf("Codeword %lX had %d bits corrected, resulting in codeword %lX.\n",g,i,l);
else
if ((j) && (i == 0))
printf("Codeword %lX has a parity error. No bits were corrected.\n",g);
else
if ((j == 0) && (i == 0))
printf("Codeword %lX does not require correction.\n",g);
break;
case 'T': /* test */
printf("Press SPACE for status, ESC to exit test...\n");
golay_test();
break;
default:
printf(errmsg);
exit(0);
}
}
/* end of G.C */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
}
}