Sunday, August 4, 2013

Jack N Poy in C++,C,Turbo C

How to make a simple jak n poy program in C++.

First of all you must have the basic knowledge in
  • If else statement.
  • Must be familiar in logical operators.
  • Deriving the variables value.
In code bellow is example of jak n poy program in c++, the process of this code: there are 2 players (player 1 and player 2). The turn starts at player 1 then player 2. The allowed inputs of both players are:
  • P – Stands for paper
  • R – meaning Rock
  • S - for scissors
Either P,R or S are the allowed inputs, if the user tries to input other character the code will terminated or end. Instead of scanf I use getche for the process of input.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define p printf
main()
{
 char p1,P1,p2,P2;
 p("Player1");
 p1=getche();
 P1= toupper(p1);
 if(P1=='P' ||P1=='S' ||P1=='R'){
  p("\n Player2");
  p2=getche();
  P2= toupper(p2);
  if(P2=='P' ||P2=='S' ||P2=='R'){
   if(P1==P2)
   p("\N Invalid input player 2");
   else if(P1=='P'&&P2=='R')
    p("\n Player1 wins!\n");
   else if(P1=='S'&&P2=='P')
    p("\n Player1 wins");
   else if(P1=='R'&&P2=='S')
    p("\n Player1 wins");
   else if(P1=='P'&&P2=='S')
    p("\n Player2 wins");
   else if(P1=='S'&&P2=='R')
    p("\n Player2 wins");
   else if(P1=='R'&&P2=='P')
    p("\n Player2 wins");
  }
  else{
  p("\n invalid");
  }
 }
 else{
  p("\N Invalid input player 1");
 }
 getch();
}
My Source code formatter : codeformatting.blogspot.com



The code toupper serve as character converter, it convert the lower case character to uppercase. So that we dont need to make any condition for the difference text format of the user. That is the simple source code of c++ jak and poy.Thank you.

2 comments: