GithubHelp home page GithubHelp logo

cs50's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

cs50's Issues

Game of fifteen

/**

  • fifteen.c
    *
  • Computer Science 50
  • Problem Set 3
    *
  • Implements the 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 [MIN,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>

// board's minimal dimension

define MIN 3

// board's maximal dimension

define MAX 9

// board, whereby board[i][j] represents row i and column j
int board[MAX][MAX];

// prototypes
void clear(void);
void greet(void);
void init(void);
void draw(void);
bool move(int tile);
bool won(void);
void save(void);

int x, y, i, j, d, tmp;

int main(int argc, string argv[])
{

// board's dimension
//int d;


// greet player
greet();

// ensure proper usage
if (argc != 2)
{
    printf("Usage: ./fifteen d\n");
    return 1;
}

// ensure valid dimensions
d = atoi(argv[1]);
if (d < MIN || d > MAX)
{
    printf("Board must be between %i x %i and %i x %i, inclusive.\n",
        MIN, MIN, MAX, MAX);
    return 2;
}

// initialize the board
init();

// accept moves until game is won
while (true)
{
    // clear the screen
    clear();

    // draw the current state of the board
    draw();

    // saves the current state of the board (for testing)
    save();

    // check for win
    if (won())
    {
        printf("ftw!\n");
        break;
    }

    // prompt for move
    printf("Tile to move: ");
    int tile = GetInt();

    // move if possible, else report illegality
    if (!move(tile))
    {
        printf("\nIllegal move.\n");

        usleep(500000);
    }

    // sleep for animation's sake
    usleep(500000);
}

// that's all folks
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("GAME OF FIFTEEN\n");
    usleep(2000000);
    }

/**

  • Initializes the game's board with tiles numbered 1 through d*d - 1,
  • (i.e., fills board with values but does not actually print them),
  • whereby board[i][j] represents row i and column j.
    */
    void init(void)
    {
    // TODO : Done

int game[d][d];

for (i = 0; i < d; i ++)
for(j = 0; j < d; j++)
{
game[i][j]= 0;

        }

}

/**

  • Prints the board in its current state.
    */

void draw(void)
{
// Draw the game tiles TODO : Done

int game[d][d];
//board size
int dim = (d * d)-1;

for (i = 0; i < d; i ++)
for(j = 0; j < d; j++)
{
game[i][j]= dim;
dim-=1;
if ( d % 2== 0 && game[i][j]==1)

            {


               x = game[i][j];
               y = game[i][j-1];
               tmp = x;
               x = y;
               y = tmp;
               game[i][j-1] = y;
               game[i][j] = x;
            }




        }

for (i = 0; i < d; i ++)
    {


        for(j = 0; j < d; j++)
            {





                 if (game[i][j] == 0)
                    {


                    printf(" ");     
                    printf(" "); 
                    printf("_");


                    }


                 else printf("%3d", game[i][j]);
                 printf(" ");
                 printf(" ");
                 printf(" ");




            }
    printf("\n"); 
    }    

}

/**

  • If tile borders empty space, moves tile and returns true, else

  • returns false.
    */
    bool move(int tile)
    {
    // TODO
    int temp;
    int posZero =0;
    int posTile = 0;
    int tileRow = 0;
    int tileCol= 0;
    int zeroRow = 0;
    int zeroCol= 0;
    int game[d][d];
    int dim = (d * d)-1;
    // int location = 0;
    //tracks location of zero tile
    for (i = 0; i < d; i++)
    {

    for (j = 0; j < d; j++)

    {
    
            game[i][j]= dim;
            dim-=1;
    
    
    
    
    
    
    
    }
    

    }

    for (i = 0; i < d; i ++)

    {

      for(j = 0; j < d; j++)
      {
    
         //search for input tile row and column
    
    
    
        //compare tile row positions
    
        if (game[i][j] == tile) 
    
          { 
            tileRow = i;
            tileCol = j;
            posTile =game[i][j];
    
    
          }
       if (game[i][j]== 0) 
    
          { 
           zeroRow = i;
           zeroCol = j;
           posZero =game[i][j];  
    
          }
    
     //swap tile is zero is to right
       if ((zeroRow==tileRow) && (zeroCol-tileCol==1))
    
          {
    
              temp = posTile;
              posTile = posZero;
              posZero =temp;
    
              return true;
          }  
    
        //swap tile is zero is to left
       if ((zeroRow==tileRow) && (tileCol-zeroCol==1))
    
          {
              temp = posTile;
              posTile = posZero;
              posZero = temp;
    
              return true;
          }
    
         //swap tile is zero is to bottom
       if ((zeroCol==tileCol) && (zeroRow-tileRow==1))
          {
              temp = posTile;
              posTile = posZero;
              posZero = temp;
    
              return true;
           }
         //swap tile is zero is to top
       if ((zeroRow==tileRow) && (tileRow-zeroRow==1))
    
          {
              temp = posTile;
              posTile = posZero;
              posZero =temp;
    
              return true;
    
        }
    
      }
    

    }

    return false;
    }

/**

  • Returns true if game is won (i.e., board is in winning configuration),
  • else false.
    */
    bool won(void)
    {
    // TODO
    return false;
    }

/**

  • Saves the current state of the board to disk (for testing).
    */
    void save(void)
    {
    // log
    const string log = "log.txt";

    // delete existing log, if any, before first save
    static bool saved = false;
    if (!saved)
    {
    unlink(log);
    saved = true;
    }

    // open log
    FILE* p = fopen(log, "a");
    if (p == NULL)
    {
    return;
    }

    // log board
    fprintf(p, "{");
    for (int i = 0; i < d; i++)
    {
    fprintf(p, "{");
    for (int j = 0; j < d; j++)
    {
    fprintf(p, "%i", board[i][j]);
    if (j < d - 1)
    {
    fprintf(p, ",");
    }
    }
    fprintf(p, "}");
    if (i < d - 1)
    {
    fprintf(p, ",");
    }
    }
    fprintf(p, "}\n");

    // close log
    fclose(p);
    }

Code freezes!!!

bool search(int value, int values[], int n)
{

int begin = 1;
int end = n;
int middle = (begin + end)/2;

while ( begin<=end && values[middle] != value)

{
if (value == values[middle])
{
return true;

    }

   else if (value > values[middle])
    {
    begin = middle + 1;

    }

   else if (value < values[middle])

   {
   end =middle -1;


   }





   else

   {

    return false;
    break;

   }

}

Vigenere: Wrong output

include <stdio.h>

include <cs50.h>

include <string.h>

include<stdlib.h>

include<ctype.h>

int main (int argc, string argv[])

{

if (argc != 2)
{

printf("Ouch missing key\n");

return 1;
}

//get value from argv array
string k= (argv[1]);

//test for non aplha

int x;

for (x = 0; x <strlen(argv[1]); x++)
{

if(isalpha(k[x]) == false)

{
printf("Ouch ensure value is alphabetical only\n");

return 1;

}

}

//int i;
//int l;
//int j;

string m;

m = GetString(); //initialise message

//iterate over the key words
for (int i = 0; i < strlen(m); i++)

{

//iterate over string key(k)

if (

    isalpha(m[i]) && isupper(m[i])

)
// m[i] >= 'A' && m[i] <= 'Z')

{

    m[i]= (m[i]-65 + (tolower(k[i % strlen(k)] ) -97)) % 26 + 65 ;

    i++; 



//printf("%s\n", m); 

} 

else if (

    isalpha(m[i]) && islower(m[i])

    )

{ 
       m[i] = (m[i] - 97 + (tolower(k[i] %strlen(k)) - 97)) % 26 + 97;

    i++; 

//printf("%s\n", m);

}

else
m[i] = m[i];

}

printf("%s\n", m);

}

Caesar Cipher - No Errors, No Prints

include <stdio.h>

include <cs50.h>

include <string.h>

include<stdlib.h>

include<ctype.h>

int main (int argc, string argv[])

{

if (argc != 2)
{

printf("Ouch missing key\n");

return 1;
}

int k= atoi(argv[1]);
int i;
int l;

string m;
m = GetString(); //initialise message

for (i = 0, l = strlen(m); i <l; i++)

{

if (

    //isalpha(m[i]) && isupper(m[i]))
    m[1] >= 'A' && m[1] <= 'Z')


{

 m[i]= (m[i]-65 + k) % 26 + 65;

}

else if (

    isalpha(m[i]) && islower(m[i])

    )

{
m[i] = (m[i] - 97 + k) % 26 + 97;

}

else
m[i] = m[i];

}

printf("%c", m[i]);

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.