Battleship game in python for a project in school

I’m a beginner at programming and I want to build a warship game in python 3.6 for my school project, I have some mistakes but I don’t know how to fix it. If you try to run my code, run it until there is a mistake because sometimes there is no problem. I used to have pop out of range in placer_bateau_verticalement function and a list index out of range mistakes. Here is my code :

 """//----------------bibliothèques----------------\""" import random    """//----------------initialisation des variables----------------\""" grid_playerA_init = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       #IA's grid (player A)                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                          0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,]  grid_playerB_init = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       #Player's grid (player B)                         0, 0, 0, 1, 0, 0, 0, 0, 0, 0,                         0, 1, 0, 1, 0, 0, 0, 1, 1, 1,                         0, 1, 0, 1, 0, 0, 0, 0, 0, 0,                         0, 1, 0, 1, 0, 0, 0, 0, 0, 0,                         0, 1, 0, 0, 0, 0, 1, 1, 0, 0,                          0, 1, 0, 0, 0, 0, 0, 0, 0, 0,                         0, 0, 0, 0, 0, 0, 0, 0, 0, 1,                         0, 0, 0, 1, 1, 1, 1, 0, 0, 1,                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,]   previous_shots = []#empty matrix which will contain all the shots touch = False #False because we don't already touch a boat NbBoatTouchPlayer = 0 #count the amount of time the player touches a boat, it will help to know the end of the game NbBoatTouchIA = 0 #count the amount of time the IA touches a boat, it will help to know the end of the game    """//----------------fonctions----------------\"""     def replaceBoat():#replace the '0' by '1' to symbolise a boat       grid_playerA_init.pop(position)     grid_playerA_init.insert(position, 1)   def can_place_boat_here(x,y, direc, length):# check if there is no collision between boats      if direc == 0: # verticle direction         for i in range(length):             if (x + y*10+10*i) < 100:#if the boat doesn t exceed                 if grid_playerA_init [x +10*y + 10*i] == 1:#if there is already an other boat                     return False              elif (x + y*10 +10*i) >= 100:#if we exceed the grid                  for j in range(length - i):                     #print((x + 10 * y) - (10 * i))                     #print (i)                     #print("can_place_boat_here")                     #print(x,y,i)                     if grid_playerA_init[(x + 10 * y) - (10* i)] == 1: #if there is already a boat                         return False          return True      if direc == 1: # horizontal direction         for i in range(length):             if ((x + 10*y + i)-9) % 10 != 0:                  if grid_playerA_init [x + 10*y + i] == 1:                     return False             elif ((x+10*y)-9) % 10 == 0:                 for j in range(length - i):                     if x+10*y-i == 1:                         return False                     return True         return True    def place_boat_vertically(x,y,direc,lenght):#place a boat vertically with the coordinates     if can_place_boat_here(x,y, direc,lenght):         for i in range(lenght):             global position             position = x + y*10             if position > 100:                 position = position- 10 * i                 for j in range(lenght-i):                     position = position - 10             #print("place_boat_vertically")             #print(x,y)             replaceBoat()             x = x + 10     else:         #####         while can_place_boat_here(x,y, direc,lenght) == False:             print("FALSE VERTC")             print(lenght)             """ -------------------------------------TEST-------------------------------------"""             x = random.randint(0, 9)             y = random.randint(0, 9)             print("x")             print(x)             print("y")             print(y)               for i in range(lenght):                 position = x + y*10                 if position > 100:                     position = position- 10 * i                     for j in range(lenght-i):                         position = position - 10                 #print("place_boat_vertically")                 #print(x,y)                 replaceBoat()                 x = x + 10      def place_boat_horizontally(x,y,direc,lenght):#place a boat horizontally with the coordinates     if can_place_boat_here(x,y, direc,lenght):         for i in range(lenght):             global position             position = x + y * 10              #print("place_boat_horizontally")             #print(x,y)              if (position - 9) % 10 == 0:                 replaceBoat()                 position = position -1 * i                 for j in range(lenght - i):                     position = position - 1                     replaceBoat()             else:                 replaceBoat()                 x = x + 1     else:        #####         while can_place_boat_here(x,y, direc,lenght) == False:             print("FALSE HORIZ")             print(lenght)             """ -------------------------------------TEST-------------------------------------"""             x = random.randint(0, 9)             y = random.randint(0, 9)             print("x")             print(x)             print("y")             print(y)                for i in range(lenght):                 position = x + y * 10                  #print("place_boat_horizontally")                 #print(x,y)                  if (position - 9) % 10 == 0:                     replaceBoat()                     position = position -1 * i                     for j in range(lenght - i):                         position = position - 1                         replaceBoat()                 else:                     replaceBoat()                     x = x + 1               """ -------------------------------------END OF THE TEST-------------------------------------"""              def check_shot(shot):#check if the shot has not already been fired     for i in range(len(previous_shots)):         if previous_shots[i] == shot:             return False     return True     def replaceshot(shot):#replace the boat touched by an X     del grid_playerB_init[shot] #delete the touched element     grid_playerB_init.insert(shot, 'X') #by an X      def IA ():     shot = random.randint(0, 99)     if check_shot(shot):         previous_shots.append(shot)          if grid_playerB_init[shot] == 1:             replaceshot(shot)             touch = True              i = 1             nextshot = shot + i             if grid_playerB_init[nextshot] == 1: # + 1 (right)                 replaceshot(nextshot)                 i = i + 1                 nextshot = shot + i               elif grid_playerB_init[nextshot] == 0: # - 1(left)                 i = 1 # variable reset                 nextshot = shot - 1                 if grid_playerB_init[nextshot] == 1:                     replaceshot(nextshot)                     i = i + 1                     nextshot = shot - i              elif grid_playerB_init[nextshot] == 0: # + 10 (upstair)                 i = 1 # variable reset                 nextshot = shot - 10                 if grid_playerB_init[nextshot] == 1:                     replaceshot(nextshot)                     i = i + 10                     nextshot = shot - i              elif grid_playerB_init[nextshot] == 0: # - 10 (downstair)                 i = 1 # variable reset                 nextshot = shot + 10                 if grid_playerB_init[nextshot] == 1:                     replaceshot(nextshot)                     i = i + 10                     nextshot = shot + i    def IA_bad(): # just a simple IA which fires randomly with no logic     shot = random.randint(0, 99)     if check_shot(shot):         previous_shots.append(shot)    def Endgame(): #function which makes end of the game when all the boats are touched by one of the players     if NbBoatTouchPlayer == 14 or NbBoatTouchIA == 14: #5+4+3+2 = 14         print("END OF THE GAME")         print("your grid :")         print(grid_playerB_init)         print("IA's grid :")         print(grid_playerA_init)    """//----------------corps du programme----------------\"""   for boat in range(2, 6): # we place the boats of 2;3;4 and 5 cases      direction = random.randint(0, 1) # choose a random integer between 0 and 1. 0 stands for vertically and 1 stands for horizontally     positionX = random.randint(0, 9)     positionY = random.randint(0, 9)     if direction == 0:         place_boat_vertically(positionX, positionY, 0, boat)     if direction == 1:         place_boat_horizontally(positionX, positionY, 1, boat) print (grid_playerA_init)    for loop in range(200):         #IA's launch     IA() print("")     print(grid_playerB_init) print("") print(previous_shots) 

Project Euler #5 in C++

I’m currently doing the Project Euler challenges to learn C++. The fifth problem is the following.

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

I thought of a loop between 20 and 10 as everything under 10 is a multiple of something under 10. At each iteration, the result is the lcm of the loop variable and the current result.

If I am correct, the complexity of this should be $ \mathcal{O}(n/2)$ . Am I right?

Is there any faster/better/cleaner implementation or another algorithm with a better complexity?

This is my code.

#include <iostream>  long gcd(long a, long b) {     long t;      if (b == 0) return a;      while (b != 0) {         t = b;         b = a % b;         a = t;     }     return a; }  long lcm(long a, long b) {     return (a * b) / gcd(a, b); }  int main() {     long n(20), result(1);     for (long i = n; i > n/2; --i) {         result = lcm(result, i);     }     std::cout << "Result: " << result << "\n"; } 

Any type of type writing data entry for your project for $5

Copy writing and writing English pages on word or converting PDF to Word or Excel data entry for quality work and happy clients. I will copy your pages on word or Excel. I can type 45 wpm with 100% accuracy. If you have any work related to data entry typing convert to PDF fell better to contact me i will try to my best service. With in 12 Hours work. Graph or Table Creation. If you have any quarry to my gig, please message me.

by: samtutorials
Created: —
Category: Data Entry
Viewed: 211


Any type of type writing data entry for your project in 48hrs for $5

Need a lot of data entry work done? Stop relying on luck to get quality freelancers and hire me! I provide high-quality Virtual Assistant services from data entry and data mining to web research and copy paste tasks. These assignments may be simple, but you need someone who intuitively understands what you want to be done and doesn’t hesitate to ask if there are areas that need to be clarified. Yes, you can find quick, excellent work done here on SEOCLEKS. you’ll realize you never have to look for any other freelancer on here for virtual assistant services. If you have a LARGE project or ongoing operational work, I’m here to help! Shoot me a message and we’ll discuss details. Here are the services I offer: Data EntryCopy Paste WorkData ConversionPDF to Excel or WordJPEG to Excel or WordTyping in Excel or Word

by: Abhishekappu
Created: —
Category: Data Entry
Viewed: 133


Project Euler #1 in C++

I’m learning C++ and currently doing Project Euler challenges. The first challenge is the following.

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

I did a quite simple and stupid brute-force implementation by just looping from 1 to 999, checking if its divisible by either 3 or 5 and if it is, sum it with the result variable.

Is there any faster/better/cleaner implementation, maybe without a loop or with less division?

#include <iostream>  int main(int argc, char *argv[]) {     int result(0);     for (int i = 1; i < 1000; ++i) {         if (!(i % 3 && i % 5)) {             result += i;         }     }     std::cout << "Result: " << result << "\n";     return 0; } 

I have a small Open Source project I’d like reviewed

Is this the proper place? It’s a node.js module intended as a possible alternative to Helmet.

If this is not the place, any suggestions? Thanks.

Hmm, apparently I must include at least three lines of code. Here’s how you would use it:

// declarative style let kepi = Kepi({   'X-Powered-By': 'super duper system',   'Content-Type': ['text/html', 'charset=utf-8']   'Feature-Policy': {     vibrate: "'none'",     geolocation: "'self'",   }, });  // can also be programmatic kepi.header('Expires').set(Date.now() + 60*60*1000);  // good for one hour  // assuming you use Express app.use(kepi.middleware()); 

Note, to mimic Hemlet you’d just go

app.use(kepi().safe().middleware());

Compared to Hemlet, its much smaller, IMO simpler, and also allows you to modify headers on the fly, and not just security headers. OTOH, I’m not a security expert and there may be bugs or flaws.

Github repo here

How to get started with AI / ML Project?

I am an experienced Full stack Developer 3+ years. I am usually work on any JS Framework as well as any PHP Framework.Also Node and Mongodb.

But Right now I got the opportunity to work on one of the new project which deals in AI and ML.

Project requirement is User will take photo from mobile and he will get the stats of shoes like brand of shoes, expected lifespan of shoes etc.

I want to know how can I proceed in this, since I am beginner in ML and AL.Also how should I manage my DB.

So from where Should I get started and at end I also want to give my inputs to Managerial end as well as clients end.

Guide Me. Thanks in anticipation.