3-pile Nim Game in Scheme

I am building a Nim game in scheme that prompt users to enter the pile number and the number of items they want to remove. The computer will simply remove 1 from the first pile, if the first pile is empty, it will remove from 2nd pile, than 3rd.

My code won’t work correctly, can someone please point out where I make mistakes? Thanks in advance.

Here is my code:

(define play-with-turns
  (lambda (game-state player)
    (display-game-state game-state)
    (cond ((over? game-state) 
           (announce-winner player))
          ((equal? player 'human)  
           (play-with-turns (human-move game-state) 'computer))
          ((equal? player 'computer)  
           (play-with-turns (computer-move game-state) 'human))
          (else  
           (error "player wasn't human or computer:" player)))))

(define computer-move
  (lambda (game-state)
    (let ((pile (if (> (size-of-pile game-state 1) 0)
                    1
                    2)))
      (display "I take 1 coin from pile ")
      (display pile)
      (newline)

      (remove-coins-from-pile game-state 1 pile))))

(define prompt
  (lambda (prompt-string)
    (newline)
    (display prompt-string)
    (newline)
    (read)))

(define human-move
  (lambda (game-state)
    (let ((p (prompt "Which pile will you remove from?")))
      (let ((n (prompt "How many coins do you want to remove?")))
        (remove-coins-from-pile game-state n p)))))

(define over?
  (lambda (game-state)
    (= (total-size game-state) 0)))

(define announce-winner
  (lambda (player)
    (if (equal? player 'human) 
        (display "You lose. Better luck next time.")
        (display "You win. Congratulations."))))

(define remove-coins-from-pile
  (lambda (game-state num-coins pile-number)
    (cond ((= pile-number 1)
           (make-game-state (- (size-of-pile game-state 1)
                               num-coins) 
                            (size-of-pile game-state 2)
                            (size-of-pile game-state 3)))
          ((= pile-number 2)
           (make-game-state (size-of-pile game-state 1)
                            (- (size-of-pile game-state 2)
                               num-coins)
                            (size-of-pile game-state 3)))
          ((= pile-number 3)
           (make-game-state (size-of-pile game-state 1)
                            (size-of-pile game-state 2)
                            (- (size-of-pile game-state 3)
                               num-coins))))))

(define exponent-of-in
  (lambda (n int)
    (if (= (remainder int n) 0)
        (+ 1 (exponent-of-in n (quotient int n)))
        0)))

(define make-game-state
  ;Returns a game state with n coins in the first pile
  ;m coins in the second pile, and k coins in the third pile.
  (lambda (n m k)
    (lambda (x)
      (cond ((= x 1) n)
            ((= x 2) m)
            ((= x 3) k)))))



(define size-of-pile
  ;Returns an integer equal to the number of coins in
  ;pile pile-number of the game-state.
  (lambda (game-state pile-number)
    (game-state pile-number)))

;; Utilities

(define display-game-state
  (lambda (game-state)
    (newline)
    (newline)
    (display "    Pile 1: ")
    (display (size-of-pile game-state 1))
    (newline)
    (display "    Pile 2: ")
    (display (size-of-pile game-state 2))
    (newline)
    (display "    Pile 3: ")
    (display (size-of-pile game-state 3))
    (newline)
    (newline)))

(define total-size
  (lambda (game-state)
    (+(+ (size-of-pile game-state 1)
         (size-of-pile game-state 2)) (size-of-pile game-state 3))))

read more

c++ encryption with rotation

I’m having some issues in creating a function that encrypts a word using a rotation number the user inputs. Here is what I have so far:

string encryptWord(string word, int num)
{
  string newWord;
  newWord = word;
  for(int i = 0; i < word.length(); i++)
    {
      newWord[i] = tolower(word[i]);
      if((word[i] >= 'a') && (word[i] <= 'z'))
        {
          newWord[i] = word[i] + (num % 26);
          if(newWord[i] > 'z')
            newWord[i] = newWord[i] - 26;

        }
    }
  return newWord;

}

now in my main when I test it out with

cout << encryptWord("xyz", 6);

the output I get is: de

Similarly, for decryption I have

string decryptRotWord(string word, int num)
{
  string newWord;
  num = num % 26;
  int index;
  for(int i = 0; i < word[i]; i++)
    {
      newWord[i] = tolower(word[i]);
      if(word[i] >= 'a' && word[i] <= 'z')
        {
          index = word[i] - num;
          if(index < 'a')
              index = index + 26;
          newWord[i] = index;
        }
    }
  return newWord;

}

read more

c++ encryption with rotation

I’m having some issues in creating a function that encrypts a word using a rotation number the user inputs. Here is what I have so far:

string encryptWord(string word, int num)
{
  string newWord;
  newWord = word;
  for(int i = 0; i < word.length(); i++)
    {
      newWord[i] = tolower(word[i]);
      if((word[i] >= 'a') && (word[i] <= 'z'))
        {
          newWord[i] = word[i] + (num % 26);
          if(newWord[i] > 'z')
            newWord[i] = newWord[i] - 26;

        }
    }
  return newWord;

}

now in my main when I test it out with

cout << encryptWord("xyz", 6);

the output I get is: de

Similarly, for decryption I have

string decryptRotWord(string word, int num)
{
  string newWord;
  num = num % 26;
  int index;
  for(int i = 0; i < word[i]; i++)
    {
      newWord[i] = tolower(word[i]);
      if(word[i] >= 'a' && word[i] <= 'z')
        {
          index = word[i] - num;
          if(index < 'a')
              index = index + 26;
          newWord[i] = index;
        }
    }
  return newWord;

}

read more

Testing 7-Zip archives from a python script

So I’ve got a python script that, at it’s core, makes .7z archives of selected directories for the purpose of backing up data. For simplicty sake I’ve simply invoked 7-zip through the windows command line, like so:

def runcompressor(target, contents):
print("Compressing {}...".format(contents))
archive = currentmodule
archive += "{}\\{}.7z".format(target, target)
os.system('7z u "{}" "{}" -mx=9 -mmt=on -ssw -up1q0r2x2y2z1w2'.format(archive, contents))
print("Done!")

Which creates a new archive if one doesn’t exist and updates the old one if it does, but if something goes wrong the archive will be corrupted, and if this command hits an existing, corrupted archive, it just gives up. Now 7zip has a command for testing the integrity of an archive, but the documentation says nothing about giving an output, and then comes the trouble of capturing that output in python.

read more